MS Word 中未处理的 COMException

COMException unhandled in MS Word

我们有一个函数可以获取word文档中查看文本的范围。但是,我们在函数的前几行中得到了 COMException。该函数每 10 秒使用固定计时器调用一次。

我们处理这个问题已经有一段时间了,非常感谢任何 help/tips,谢谢。

private void GetViewedText()
    {
        // get the rectangle shown on the screen
        IntPtr h = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

        h = FindWindowExW(h, new IntPtr(0), "_WwF", "");        
        h = FindWindowExW(h, new IntPtr(0), "_WwB", this.Application.ActiveDocument.Name);

        h = FindWindowExW(h, new IntPtr(0), "_WwG", "Microsoft Word Document");       

        RECT tagRect = new RECT();
        GetWindowRect(h, out tagRect);
        //-------------------------------------------------------------------------------------------------------

        //get the range in the doc from the rect
        // problem with "ActiveDocument" or "ActiveWindow"
        Word.Range r1 = (Word.Range)this.Application.ActiveWindow.RangeFromPoint(tagRect.left, tagRect.top); 

        Word.Range r2 = (Word.Range)this.Application.ActiveWindow.RangeFromPoint(tagRect.right, tagRect.bottom);

        Word.Range r = this.Application.ActiveDocument.Range(r1.Start, r2.Start);
        //-------------------------------------------------------------------------------------------------------

        //for each paragraph we increase the number of times read it and change the color
        for (int p = 1; p <= r.Paragraphs.Count; p++)
        {
            Word.Range rPar = document.Range(r.Paragraphs[p].Range);
            Word.Range rParNum = document.Range(rPar.Start, rPar.Start + 1);
            Word.Range rParText = document.Range(rPar.Start + 1, rPar.End);
            try
            {
                int parNum = Int32.Parse(rParNum.Text);
                paragraphs[parNum].ReadParagrafh();
                if (paragraphs[parNum].GetCounter() == 1)
                {
                    rParText.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                }
                else if (paragraphs[parNum].GetCounter() == 2)
                {
                    rParText.HighlightColorIndex = Word.WdColorIndex.wdBrightGreen;
                }
                else
                {
                    rParText.HighlightColorIndex = Word.WdColorIndex.wdGreen;
                }
            }
            catch
            {
                continue;
            }
        }
    }

您是否因为基于 0 的索引引用而崩溃?

for (int p = 1; p <= r.Paragraphs.Count; p++)

改为

for (int p = 0; p < r.Paragraphs.Count; p++)

这么多东西都是0基础的。假设您有 4 个段落。在文档内部,它们是否被引用为

paragraph[0], paragraph[1], paragraph[2], paragraph[3]

paragraph[1], paragraph[2], paragraph[3], paragraph[4]

段落的数量可能是 4,但引用为 0-3...事情可能工作正常,例如:10 个段落,并且它从 1-9 全部有效(因为 10 个条目本来是 0 -9),但是当你尝试第 10 个时,不存在这样的参考。

来自评论的反馈...

由于是基于 1 的,也许您应该尝试预先检查范围的 NULL?

if( r.Paragraphs[p].Range == null )
   continue;

Word.Range rPar = document.Range(r.Paragraphs[p].Range);

修复它,不得不删除 document.Range 所以新行是:

Word.Range rPar = r.Paragraphs[p].Range;