如何在microsoft.interop.word中获取光标位置? [英] How to get the cursor position in microsoft.interop.word ?

查看:193
本文介绍了如何在microsoft.interop.word中获取光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用

Hi,

I am using

using Microsoft.Office.Interop.Word;

汇编以便访问word文件并做一些事情。我想知道是否有任何方法可以获得光标当前所在的角色位置。



我尝试了什么:



以下是我使用的代码



assembly in order to access the word files and do some stuff. I want to know is there any way that I can get the character position where the cursor is currently in.

What I have tried:

Below is the code I use

Object wordObject = null;
           Microsoft.Office.Interop.Word.Application word = null;
           Document document = null;

           try {
               wordObject = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

               word = (Microsoft.Office.Interop.Word.Application)wordObject;
               word.Visible = false;
               word.ScreenUpdating = false;
               string fullPath = word.ActiveDocument.FullName;

               document = word.Documents.Open(fullPath);

               int count = document.Words.Count;
               for(int k = 1; k <= count; k++)
               {
                   string text = document.Words[k].Text;
                   MessageBox.Show(text);
               }

               if (document.Paragraphs.Count > 0)
               {
                   var paragraph = document.Paragraphs.First;
                   var lastCharPos = paragraph.Range.Sentences.First.End - 1;
                   MessageBox.Show(lastCharPos.ToString());
               }
               word.Quit();
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }





这里我得到第一段的最后一个字符位置。但是我想获得当前光标所在的字符位置。谢谢你提前!



here I get the last character position of the first paragraph. But I want to get the character position where the cursor is located at the moment. Thanks in advanced!

推荐答案

你的应用程序打开了活动文档的第二个副本......没有有意义的光标信息。
Your app openened a SECOND copy of the "active document" ... there is no meaningful cursor info.

这篇关于如何在microsoft.interop.word中获取光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆