none
Microsoft.Office.Interop.Word.Application.Selection is null if when opening a document with Visible = false RRS feed

  • Question

  • Hello,

    I have a list of words that I need to highlight in a doc using C# code.

    Everything works fine if when I open the doc the property visible is set to true.

    When the property visible is set to false the selection property is null and I can't highlight the text.

    I need to highlight the text in the back before I display the doc in a viewer I use so I can't use the visible = true.

    Please look at the attached code and tell me how can I make it work

     

    Microsoft.Office.Interop.Word.Application a = new Microsoft.Office.Interop.Word.Application();                        
    
    Document oDoc = a.Application.Documents.Open(fileName, ReadOnly: false, Visible: false);                        
    
    List<string> words = new List<string>();
    
    words.Add("me");
    
    words.Add("you");
    
    words.Add("us");
    
    foreach (string word in words)
    
    {
    
    	SelectionFind(word, a);
    
    }
    
    oDoc.Save();
    
    private void SelectionFind(string piWord, Microsoft.Office.Interop.Word.Application piWordApp)
    
    {                  
    
    	piWordApp.Selection.Find.ClearFormatting();
    
            piWordApp.Visible = true;
    
            piWordApp.Selection.Find.Replacement.ClearFormatting();
    
            piWordApp.Selection.Find.Replacement.Highlight = 1;
    
            piWordApp.Selection.Find.Text = piWord;
    
            piWordApp.Selection.Find.Replacement.Text = piWord;
    
            piWordApp.Selection.Find.Forward = true;
    
            piWordApp.Selection.Find.Wrap = WdFindWrap.wdFindContinue;
    
            piWordApp.Selection.Find.Format = true;
    
            piWordApp.Selection.Find.MatchCase = false;
    
            piWordApp.Selection.Find.MatchWholeWord = false;
    
            piWordApp.Selection.Find.MatchWildcards = false;
    
            piWordApp.Selection.Find.MatchSoundsLike = false;
    
            piWordApp.Selection.Find.MatchAllWordForms = false;
    
            piWordApp.Selection.Find.Execute(Replace: WdReplace.wdReplaceAll);
    
            piWordApp.Options.DefaultHighlightColorIndex = WdColorIndex.wdYellow;
    
            piWordApp.Selection.Range.HighlightColorIndex = WdColorIndex.wdYellow;
    
    }
    

    Thanks,

    Orit

     

     

    Monday, December 19, 2011 9:53 AM

Answers

  • Hi Orit

    Don't use Selection.Find, use Range.Find. And apply the highlighting as part of the replacement. Something like this:

      Word.Range rngFind = piWordApp.ActiveDocument.Content;
      piWordApp.Options.DefaultHighlightColorIndex = WdColorIndex.wdYellow;
      rngFind.Find.ClearFormatting();
      rngFind.Replacement.Highlight = true;
      //and so on...
      rngFind.Execute(Replace: WdReplace.wdReplaceAll);


    Cindy Meister, VSTO/Word MVP
    Monday, December 26, 2011 7:35 AM
    Moderator

All replies

  • Forgot to add:

    I'm using VS2010 and Word2010

    Monday, December 19, 2011 9:54 AM
  • Hi Orit,

     

    Thanks for posting in the MSDN Forum.

     

    The key of you issue is set the Visible property as false when you open the document. If you set the Visible as true and set application’s visible as false your document will run on background too. And the exception will disappear.

     

    I hope what I said can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 21, 2011 5:25 AM
    Moderator
  • Hi Orit

    << have a list of words that I need to highlight in a doc using C# code.

    Everything works fine if when I open the doc the property visible is set to true.

    When the property visible is set to false the selection property is null and I can't highlight the text.

    I need to highlight the text in the back before I display the doc in a viewer I use so I can't use the visible = true.>>

    I don't understand why, if you need to select all these words, the document shouldn't be visible. A Selection doesn't make sense in a document that's not visible? What do you need to do with these words?


    Cindy Meister, VSTO/Word MVP
    Wednesday, December 21, 2011 8:07 AM
    Moderator
  • Hi Tom,

    I tried what you suggested and it doesn't work.

    When the property is set to true the doc is displayed no matter if I set the application visible to false or not.

     

    Have a nice day,

    Orit

    Sunday, December 25, 2011 11:44 AM
  • Hi Cindy,

    These words are a list returned from a query engine. I need to highlight to words to the user.

    I'm using a viewer in my application that show the highlighted doc.

    For that I need to take the list of words, find them in the doc, highlight them and display it in my viewer and not in MS word.

    That is why I want to do everything in the background before I'm displaying the doc.

     Orit

    Sunday, December 25, 2011 11:48 AM
  • Hi Orit

    Don't use Selection.Find, use Range.Find. And apply the highlighting as part of the replacement. Something like this:

      Word.Range rngFind = piWordApp.ActiveDocument.Content;
      piWordApp.Options.DefaultHighlightColorIndex = WdColorIndex.wdYellow;
      rngFind.Find.ClearFormatting();
      rngFind.Replacement.Highlight = true;
      //and so on...
      rngFind.Execute(Replace: WdReplace.wdReplaceAll);


    Cindy Meister, VSTO/Word MVP
    Monday, December 26, 2011 7:35 AM
    Moderator
  • Hi  Cindy,

    I can not use Range.Find because ActiveDocument throws an exception "This command is not available because no document is open"

    the visible false is problematic from all sides available.

     

    Orit

    Tuesday, December 27, 2011 10:03 AM
  • Hi Orit

    I did say "something like this", but code does need to be adapted to your specific situation. Instead of ActiveDocument, use you document object oDoc


    Cindy Meister, VSTO/Word MVP
    Tuesday, December 27, 2011 12:07 PM
    Moderator
  • After calling Documents.Open it returns a null? Any idea?

    I'm running Windows 7 hosting the test solution in a web part in my SharePoint 2010 environment and the files on my C: drive. I've set all the permissions etc.

    • Edited by nuclear Sunday, April 1, 2012 2:26 PM
    Sunday, April 1, 2012 2:23 PM