Share via

How to find multiple words in a Word document?

Anonymous
2024-08-05T15:12:19+00:00

I'm asking a question that has been asked before because the previous topic has been closed. I have the same problem as Walkecba in the old topic. Dim DocSource As Document, DocTarget As Document

Dim rng As Range

Dim FD As FileDialog

Dim strFileName As String

Dim i As Long

Dim rngKeyword As Range

Dim strKeyword As String

Set DocTarget = ActiveDocument

Set FD = Application.FileDialog(msoFileDialogFolderPicker)

With FD

    .Title = "Select the file containing the key words."

    .AllowMultiSelect = False

    If .Show = -1 Then

        strFileName = .SelectedItems(1)

    Else

        MsgBox "You did not select the file containing the key words."

        Exit Sub

    End If

End With

Set DocSource = Documents.Open(strFileName)

With DocSource

    For i = 1 To .Paragraphs.Count

        Set rngKeyword = .Paragraphs(i).Range

        rngKeyword.MoveEnd wdCharacter, -1

        strKeyword = rngKeyword.Text

        If Instr(DocTarget.Range, strKeyword) > 0 Then

            rngKeyword.Font.ColorIndex = wdGreen

        Else

            rngKeyword.Font.ColorIndex = wdRed

        End If

    Next i

End With

I am trying to use the above code for macro but when I try to run the macro it stops at the point of “select the file containing the key words.”  I can not select the Word document file to continue running the macro.

https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-find-multiple-words-in-a-ms-word-document/24601fee-83f6-4eca-82c3-cdba4daa1ef7

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2024-08-05T15:35:54+00:00

    The problem is that the posted code uses the wrong name for the FileDialog function's argument. Instead of msoFileDialogFolderPicker (which creates a dialog that only allows picking a folder), it should use msoFileDialogFilePicker to allow choosing a file.

    Set FD = Application.FileDialog(msoFileDialogFilePicker)
    
    2 people found this answer helpful.
    0 comments No comments
  2. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2024-08-06T17:57:53+00:00

    OK, to test for a prefix as you explained it, you can use code like this:

    If Left(LCase(DocTarget.Range.Text), Len(strKeyword)) = LCase(strKeyword) Then

    This compares the strKeyword to the leftmost part of the DocTarget.Range.Text for the same length as strKeyword, using only the lower-case version of each string.

    For example, if the target string is "acted" and the keyword is "act", the comparison will look at only the first three characters and return True for the comparison. But if the target string is "fact", the comparison of "act" and "fac" returns False.

    1 person found this answer helpful.
    0 comments No comments
  3. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2024-08-06T12:24:22+00:00

    The macro is case-sensitive because the Instr function is (by definition) case-sensitive. The solution is to compare the all-lower-case versions of the two strings (or all-upper-case would also work). The LCase function makes that conversion (this does not change anything in the document, only in VBA's memory).

    To make the macro not case-sensitive, change this statement

     If Instr(DocTarget.Range, strKeyword) > 0 Then
    

    to this:

     If Instr(LCase(DocTarget.Range.Text), LCase(strKeyword)) > 0 Then
    

    I can suggest a partial solution for the other request: In a similar way, you can use the Replace function to remove punctuation and white space from each of the strings before comparing them. That's a bit more complicated than the LCase situation, because you need to tell it which punctuation to remove.

    I don't understand what "prefix" you referred to.

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2024-08-06T13:30:27+00:00

    Thank you very much for your help. What I mean by matching the prefix is ​​that the word to be searched starts with the source word. For example act. Should not include "fact" while searching, but can include "acted".

    0 comments No comments
  5. Anonymous
    2024-08-06T11:35:25+00:00

    The problem is that the posted code uses the wrong name for the FileDialog function's argument. Instead of msoFileDialogFolderPicker (which creates a dialog that only allows picking a folder), it should use msoFileDialogFilePicker to allow choosing a file.

    Set FD = Application.FileDialog(msoFileDialogFilePicker)
    

    The code worked, and I'm grateful to you for that. Does not differentiate between uppercase and lowercase when searching for words in text. It turns green if the source word is exactly the same as how it is written in the document. How can I search for the same word regardless of whether it is written in uppercase or lowercase?

    I'm asking a lot from you, but I have one more request. How can I add "match prefix, ignore punctuation characters and ignore white-space character" search features to this code? Thank you very much in advance.

    0 comments No comments