I search a Word document for a specific word occurring at the beginning of a paragraph. It works.
I'd like to search for two words at the same time.
Word1 or Word2 is found at the start of a paragraph
Word1 or Word2 starts consecutive paragraphs
If I run the sub twice, once for Word1 then once for Word2, it won't find instances where Word1 starts a paragraph and Word2 starts the next consecutive one.
I'd like to implement this logic:
(search_text = Chr(13) & "Word1") OR (search_text = Chr(13) & "Word2")
Sub Para_start() search_text = Chr(13) & "Word1" With W_rg.Find While .Execute If W_rg.Find.Found Then Msgbox "Found" & Word1 'Do more stuff, for example 'Insert a comment where Word1 or Word2 is found 'Insert another comment if EITHER Word1 or Word 2 are found in consecutive paragraphs End If Wend End With End Sub
No, it isn't that simple. Find works the same in VBA as it does in the UI - there is no option for using OR. However, finding Chr(13) & "Word1" and then checking the following paragraph to see if it starts with Word2 is simple, for example:
Sub Para_start() Dim search_text1 As String: search_text1 = Chr(13) & "Word1" Dim search_text2 As String: search_text2 = "Word2" Dim findRange As Range: Set findRange = ActiveDocument.Content With findRange With .Find .ClearFormatting .Text = search_text1 End With Do While .Find.Execute If findRange.Paragraphs(1).Next.Range.Words(1).Text = search_text2 Then 'Insert a comment where both Word1 and Word 2 are found in consecutive paragraphs Else 'Insert a comment where Word1 is found End If .Collapse wdCollapseEnd Loop End With End Sub