Thanks in advance for taking the time to read this.
I would like to code the Find function in Word 2013 for searching specific words in multiple styles. Not even sure if this is possible because Word doesn't have that option in Advanced Find --> More --> Format --> Style. It only allows for filtering one style.
My goal is to be able to find paragraph marks (syntax: ^p) on styles 'Heading 1' through 'Heading 9'.
Sub AppendixFix()
' Declaring variables
Dim multiStyles As String, i As Integer
multiStyles = "Heading 1, Heading 2, Heading 3, Heading 4, Heading 5, Heading 6, Heading 7, Heading 8, Heading 9"
' Start at the top of document and clear find formatting
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
' Navigate to Appendix section
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = "Appendix"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.Execute
End With
' Loop until find is not found and limit to 1000 counts
Do While Selection.Find.Found = True And i < 1000
i = i + 1
' Add text to the beginning of each line
Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:=" *Test* "
' Navigate to the next heading by looking at following paragraph mark
Selection.Find.Style = ActiveDocument.Styles(multiStyles)
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindStop
.Format = True
.Execute
.Execute
End With
Loop
End Sub
I expected the code to start inputting Test at the first 'Appendix' heading, then inputs Test to its sub-headings (Heading 2, 3..., 9), and continues to the end of the document. However, it only adds the text to Heading 1-styled headers skipping its sub-headers. It seems to me that only the first style in the list gets read in. I've tried removing Heading 1 from the list and it checks for Heading 2-styled headers.
.Style
property of the selected text. You could sayIf InStr(multiStyles, Selection.Style) Then
to test theTrue
condition. – dwirony