Search code examples
vbabuttonms-wordfind

Find text in document


I created VBA code in MS Word to search for a selected word among all the titles in my document that have the same formatting.

I'm working on a document with more than 500 titles. I need to find text under each of these titles. (The document lists the names of the local counties in each district, the titles being the name of each district.)

I write the name of the county I want to search, select that name with the mouse, and run the macro using the shortcut I put in the Quick Access menu.

I want to share this document with my colleagues. To work on their computers, I would have to install a similar shortcut on each one, which is not practical.

I created a MacroButton to run this macro. The problem is that, after selecting the text I want to search, when I double click the MacroButton, the selection disappears, resulting in a search for an empty value.

How can I create a VBA macro I can associate with a macro button, so it will work on every computer that opens this document?

The ideal scenario is the macro would search for the text inserted on a given line of the document (I suggest the first line, where users would insert the name they want to search) without the need of selecting the text to be searched.

Sub PROC()
'
' PROC Macro
'
'
    Selection.Copy
    Selection.Find.ClearFormatting
    Selection.Find.Font.Size = 18
    With Selection.Find
        .Text = Selection.Text
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub

Something like:

Print screen of MacroButton
enter image description here


Solution

  • When creating Word documents you should always use styles, especially for headings/titles - for 16 reasons why see https://shaunakelly.com/word/numbering/usebuiltinheadingstyles.html

    This will help your colleagues as they will then be able to use the Navigation pane to view the headings in the document as well as using your macro.

    As an example for your macro using Heading 1 style:

    Sub PROC()
        Dim textToFind As String
        textToFind = InputBox(Prompt:="Type name of district you want to find", Title:="Find district")
        With ActiveDocument.Content
            With .Find
                .ClearFormatting
                .Text = textToFind
                .Style = wdStyleHeading1
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindAsk
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            If .Find.Execute Then .Select
        End With
    End Sub