VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)

目的:得到在Word、Excel、PPT、Outlook中选中的一段内容。

Word:

        private string GetSelectCont()
        {
            string w = "";
            Word.Selection sec = appWord.Selection;
            Word.Words wds = sec.Words;           
            w = wds.Application.Selection.Text;
            return w.Trim();
        }

Outlook:

       private string GetSelectCont(Outlook.Inspector Inspector)
       {
            string w = "";

            Word.Document document = Inspector.WordEditor;           
            w = document.Application.Selection.Words.Application.Selection.Text;
           
            return w.Trim();
       }

      注:无法得到标题等之类的选中内容。

PPT:

        private string GetSelectCont()
        {
            string w = "";

            PowerPoint.Selection sec = appPPT.ActiveWindow.Selection;
            string word = sec.TextRange.Text;
            return word.Trim();
        }

Excel:

         private string GetSelectCont()
        {
            string w = "";
            object[,] result;
            object res1;
            string res2;
            Excel.Workbook wbook = Globals.ThisAddIn.Application.ActiveWorkbook;  //当前活动workbook
            Excel.Worksheet wsheet = (Excel.Worksheet)wbook.ActiveSheet;          //当前活动sheet 
            Excel.Range range = (Excel.Range)wsheet.Application.Selection;      //当前选中的cells

            int count = range.Count;
            res1 = (object)range.Value2;
            //如果选中多个单元格
             if (count > 1)
             {
                 int row_count = range.Rows.Count;
                 int col_count = range.Columns.Count;
                 result = (object[,])res1;
                 for (int i = 1; i <= row_count; i++)
                 {
                     for (int j = 1; j <= col_count; j++)
                     {
                         if (result[i, j] != null)
                         {
                             w += (string)result[i, j] + " ";
                         }
                     }
                 }
             }
             else {
                 //如果选中单个
                 if (res1 == null)
                 {
                     w = "";
                 }
                 else {                   
                     res2 = res1.ToString();
                     w = res2;
                 }
             }
            return w.Trim();
        }

       注:Excel比较复杂,这只能得到选中单个或多个单元格的内容,却不能得到某个单元格中mark起来的内容,正在查找解决办法。。。

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值