Consulting

Results 1 to 2 of 2

Thread: MS Word Search for Multiple Words

  1. #1
    VBAX Newbie
    Joined
    Aug 2021
    Posts
    1
    Location

    MS Word Search for Multiple Words

    Hi,

    The script I have currently searches for two words but I would like it to search for five words.

    The other three words I would like to search are "Audio-^#^#^#^#^#", "Photo-^#^#^#^#^#" and "Video-^#^#^#^#^#"

    The script below will find two words and works but how can I amend it other than running it two more times to include the additional 3 search terms?


    Sub FindSourceDocs1()
    Dim FoundResult As Boolean
    Dim pageserial, SearchSeq, SearchSingle As String
    Dim PgColour As WdColor
    Dim CurrentDoc As String
    Dim FndText As String
    Application.ScreenUpdating = False
    
    
    CurrentDoc = ActiveDocument.Name
    
    
    FoundResult = True
    
    
    SearchSeq = "Doc-^#^#^#^#^#"
    SearchSingle = "Task-^#^#^#^#^#"
    
    
    PgColour = wdColorRed 'RED
    
    
    
    
    Do Until FoundResult = False
    
    
            Selection.Find.ClearFormatting
           
            Selection.Find.Font.Color = PgColour
            
            Selection.Find.Text = SearchSeq
            Selection.Find.Wrap = wdFindContinue
            
            If Selection.Find.Execute = True Then
                pageserial = LTrim(Selection)
                Documents("SourceDoc.docx").Activate
                Selection.HomeKey Unit:=wdStory
                
                Selection.Find.ClearFormatting
                            
                Selection.Find.Text = pageserial
    
    
                If Selection.Find.Execute = True Then
                
                    Selection.Rows(1).Select
                    Selection.Copy
                   
                 
                    Documents(CurrentDoc).Activate
                    
                Selection.Find.Text = pageserial
                
                           
                With Selection.Find
                    Selection.Paste
                    
                   End With
                   
                Else
                    Selection.HomeKey Unit:=wdStory
                    Windows(CurrentDoc).Activate
    
    
                    If PgColour = wdColorGreen Then Selection.Font.Color = wdColorRed Else: Selection.Font.Color = wdColorGreen
                End If
            Else
                If FoundResult = True And SearchSeq = "Doc-^#^#^#^#^#" Then
                    SearchSeq = SearchSingle
                    FoundResult = True
                Else
                    FoundResult = False
                   
                End If
            End If
        Loop
    Application.ScreenUpdating = True
    End Sub
    Last edited by Paul_Hossler; 08-10-2021 at 01:58 PM.

  2. #2
    Seehttps://www.gmayor.com/document_batch_processes.htm which will search for any number of words.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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