Results 1 to 4 of 4

Thread: [RESOLVED] Finding occurences of a word in Microsoft Word

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved [RESOLVED] Finding occurences of a word in Microsoft Word

    I use the following to find a word in a word document opened in a DSOFramer ActiveX control:
    Code:
    oDoc = ctl.ActiveDocument
    oDoc.ActiveWindow.Selection.WholeStory()
    oDoc.ActiveWindow.Selection.Find.ClearFormatting()
     oDoc.ActiveWindow.Selection.Find.Replacement.ClearFormatting()
    With oDoc.ActiveWindow.Selection.Find
          .Text = "control"
          .Forward = True
          .Wrap = WdFindWrap.wdFindContinue
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
     End With
    
    oDoc.ActiveWindow.Selection.Find.Execute()
    The code finds and highlights the first occurence of the word "control" but ignores all of the other occurences.
    What am I doing wrong ? I must be missing something easy.

    Parksie

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Finding occurences of a word in Microsoft Word

    Leave your current code in place, but instead of doing the Selection.Find.Execute(), do a
    Code:
        Selection.Find.HitHighlight "control"

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Finding occurences of a word in Microsoft Word

    If you Record a Macro it will usually show you the code office apps use to complete the desired task.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Finding occurences of a word in Microsoft Word

    Quote Originally Posted by RobDog888
    If you Record a Macro it will usually show you the code office apps use to complete the desired task.
    That's the way I usually do it, but I had to figure out the HitHighlight because the recording didn't happen to catch that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width