none
'Unknown CAPI error' encountered when word from Comment is selected RRS feed

  • Question

  • Hi,

    I am a VSTO developer.

    Currently i am developing a tool that works with Office 2007.

    The tool is for MS word. It will seach the entire document for some words. If it finds the it would select the word and pop up a dilaog asking the user to replace it with some approriate option of just ignore it( Just like the Find and Replace function)

    Currently i am seraching the entire document word by word.

    It works fine for all except the Comments part.

    The code is :

    foreach (MSWord.Comment comment in Globals.ThisAddIn.Application.ActiveDocument.Comments)
                    {
                      
                        foreach (MSWord.Range word in comment.Range.Words)
                        {
                           word.Select();
                        }
                   }

     

    The Select() method gives the 'Unknown CAPI error'.

    I have also observed that if the cursor is placed inside the comment then the error is not thrown. But if the cursor is placed in some other location and not inside the comment it throws this error.

    Is there any way by which i can keep my cursor placed inside the comment part so that while we do the word.Select() error is not thrown? Or any other way by which we can resolve it.

    Please suggest.

    Thanks in Advance.

    Saheli

     

    Tuesday, September 13, 2011 2:33 PM

Answers

  • Hi Saheli

    OK, I'm able to reproduce the problem from within VSTO. It appears Word's not able to change the screen when VSTO is running the code. But if I switch views for it, then I get no error:

            private void btnSelectComments_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                doc.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdNormalView;
                doc.ActiveWindow.View.SplitSpecial = Microsoft.Office.Interop.Word.WdSpecialPane.wdPaneComments;
                foreach (Word.Comment comment in doc.Comments)
                {
    
                    foreach (Word.Range word in comment.Range.Words)
                    {
                        word.Select();
                    }
                }
    

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Wednesday, September 21, 2011 8:41 AM
    Friday, September 16, 2011 7:01 AM
    Moderator

All replies

  • Hi Saheli

    I think we need more information about the state of the document where you're doing this. I ran some quick tests in VBA (as that tells us if this is a basic problem in Word or not) and didn't get any problems. But I did notice a couple of things happening in the UI...

    How are comments displayed in the document before you run your code? Are they in a panel in "balloons"? Are they only viewable if the user hovers over them with the mouse in the text? Are they being viewed in the "Reviewing pane"?

    At the point where your code stops, if you look at the document, has the view changed at all?

    When I do what you're trying, starting in "Print Layout", the View switches to the Normal (Draft) view and the code is running in the old Comments Pane.

    To work on this, I think we need to know where you're starting from...


    Cindy Meister, VSTO/Word MVP
    Friday, September 16, 2011 6:47 AM
    Moderator
  • Hi Saheli

    OK, I'm able to reproduce the problem from within VSTO. It appears Word's not able to change the screen when VSTO is running the code. But if I switch views for it, then I get no error:

            private void btnSelectComments_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                doc.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdNormalView;
                doc.ActiveWindow.View.SplitSpecial = Microsoft.Office.Interop.Word.WdSpecialPane.wdPaneComments;
                foreach (Word.Comment comment in doc.Comments)
                {
    
                    foreach (Word.Range word in comment.Range.Words)
                    {
                        word.Select();
                    }
                }
    

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Wednesday, September 21, 2011 8:41 AM
    Friday, September 16, 2011 7:01 AM
    Moderator