-1

We have a function which is supposed to get the range of the viewed text in a word document. However, we are getting the COMException in the first few lines of the function. The function is called with a fixed timer every 10 seconds.

We're dealing with this for quite some time and would appreciate any help/tips, Thank you.

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;
            }
        }
    }
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
lironlahav
  • 13
  • 3
  • 1
    For starters, which line is throwing the exception? And what is the exception message? – Yuval Itzchakov Mar 16 '15 at 13:59
  • When we run it the line that throws the exception is: Word.Range rPar = document.Range(r.Paragraphs[p].Range); The exception is COMException was unhandled by user code (I can't find a way to add a print screen): An exception of type 'System.Runtime.InteropServices.COMException' occurred in WordAddIn1.dll but was not handled in user code Additional information: Type mismatch – lironlahav Mar 16 '15 at 14:56

2 Answers2

0

Are you crashing because of 0-based index reference?

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

change to

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

So many things are 0-based. Lets say you have 4 paragraphs. Internally to the document, are they referenced as

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

or

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

The COUNT of paragraphs may be 4, but referenced as 0-3... Things may have worked fine, ex: 10 paragraphs, and it worked from 1-9 all valid (since 10 entries would have been 0-9), but then when you tried the 10th, no such reference exists.

Feedback from comment...

Since 1-based, maybe you should try pre-checking for NULL of the range?

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

Word.Range rPar = document.Range(r.Paragraphs[p].Range);
DRapp
  • 47,638
  • 12
  • 72
  • 142
0

fixed it, had to remove the document.Range so the new line is:

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

lironlahav
  • 13
  • 3