none
Microsoft.Office.Interop.Word.Find.Excecute: different behaviour between Word 2010 and Word 2016 RRS feed

  • Question

  • Hi everyone,

    we have a VSTO-AddIn for Word and we are using the Find interface to search some of our hidden token in an opened Document.

                    rng.Find.ClearFormatting();
                    rng.Find.Font.Hidden = Convert.ToInt16(true); // Our token
                    rng.Find.Forward = forward;
                    rng.Find.Text = patternToFind; // for example \<OURHIDDENTOKEN*\>
                    rng.Find.MatchWildcards = true;
                    rng.Find.Execute(); // After this operation the visual selection disappears

    When the user had selected a text and we do this operation the selection disappears. At least it looks like that because the text isn't visually selected anymore. In the background its still selected so you can cut the "selected" text for example.

    "rng.Select()" after the "rng.Find.Execute()" sets visual back to the selection but the selection now flickers.

    This wasn't a problem in Word 2010.

    Thanks for your help,

    Martin


    Thursday, August 10, 2017 6:40 AM

Answers

  • Hi Da_Martin,

    We could record current selected range and then select the whole content. If we do not find anything, then we could select back to previous selected range. We could use Application.ScreenUpdate to hide the process for users so they won't see we have ever selected the whole content.

    Here is the example.

                Globals.ThisAddIn.Application.ScreenUpdating = false;

                Word.Range savedRng = Globals.ThisAddIn.Application.Selection.Range;

                Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdMainTextStory].Select();

                Word.Selection rng = Globals.ThisAddIn.Application.Selection;

     

                rng.Find.ClearFormatting();

                rng.Find.Font.Hidden = Convert.ToInt16(true); // Our token

                rng.Find.Forward = false;

                rng.Find.Text = "Test"; // for example \<OURHIDDENTOKEN*\>

                rng.Find.MatchWildcards = true;

                rng.Find.Execute();

                if (!rng.Find.Found) {

                    savedRng.Select();

                }

                Globals.ThisAddIn.Application.ScreenUpdating = true;

    Best Regards,

    Terry

    • Marked as answer by Da_Martin Tuesday, August 22, 2017 12:19 PM
    Thursday, August 17, 2017 7:39 AM
  • Hi Terry,

    just turning the ScreenUpdating off and after the search on again solves the problem with the Find.Excecute().

    Thank you for your help!

    Martin

    • Marked as answer by Da_Martin Tuesday, August 22, 2017 12:20 PM
    Tuesday, August 22, 2017 12:19 PM

All replies

  • Hi Da_Martin,

    I could reproduce your issue. I defined rng as a Range Object and reproduced your issue. I suggest you use Selection.Find to find the text which does not have the issue.

    Word.Selection rng = Globals.ThisAddIn.Application.Selection;

                rng.Find.ClearFormatting();

                rng.Find.Font.Hidden = Convert.ToInt16(true);

                rng.Find.Forward = false;

               rng.Find.Text = "Test";

                rng.Find.MatchWildcards = true;

                rng.Find.Execute();

    Best Regards,

    Terry

    Monday, August 14, 2017 4:09 AM
  • Hi Terry,

    thank you for your time!

    I can't use Selection because I want to know all token in the Document. So we use something like that to get the Range "Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory]" or "Globals.ThisAddIn.Application.ActiveDocument.Content". We need it to find all our tocken not only the once inside the current selection.

    Is there another way or will there be a fix for word?

    Thanks,

    Martin 
    Tuesday, August 15, 2017 11:19 AM
  • Hi Da_Martin,

    You could select all the content at first.

     Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdMainTextStory].Select();
     Word.Selection rng = Globals.ThisAddIn.Application.Selection;

    Best Regards,

    Terry

    Wednesday, August 16, 2017 9:56 AM
  • Hi Terry,

    the Select()-Call selects all the Text so the users sees that there is going on something. We want to "Find" things in the background so that the selection doesn't flicker.

    Maybe there is another way to search in the opened document we could use?

    Or maybe there will be a fix of this behaviour?

    Thank you,

    Martin

    Wednesday, August 16, 2017 10:26 AM
  • Hi Da_Martin,

    We could record current selected range and then select the whole content. If we do not find anything, then we could select back to previous selected range. We could use Application.ScreenUpdate to hide the process for users so they won't see we have ever selected the whole content.

    Here is the example.

                Globals.ThisAddIn.Application.ScreenUpdating = false;

                Word.Range savedRng = Globals.ThisAddIn.Application.Selection.Range;

                Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdMainTextStory].Select();

                Word.Selection rng = Globals.ThisAddIn.Application.Selection;

     

                rng.Find.ClearFormatting();

                rng.Find.Font.Hidden = Convert.ToInt16(true); // Our token

                rng.Find.Forward = false;

                rng.Find.Text = "Test"; // for example \<OURHIDDENTOKEN*\>

                rng.Find.MatchWildcards = true;

                rng.Find.Execute();

                if (!rng.Find.Found) {

                    savedRng.Select();

                }

                Globals.ThisAddIn.Application.ScreenUpdating = true;

    Best Regards,

    Terry

    • Marked as answer by Da_Martin Tuesday, August 22, 2017 12:19 PM
    Thursday, August 17, 2017 7:39 AM
  • Hi Terry,

    just turning the ScreenUpdating off and after the search on again solves the problem with the Find.Excecute().

    Thank you for your help!

    Martin

    • Marked as answer by Da_Martin Tuesday, August 22, 2017 12:20 PM
    Tuesday, August 22, 2017 12:19 PM