dc-ddfe
dc-ddfe

Reputation: 495

MS Office wildcards - how do I handle 0 or 1 occurrences of a character?

I'm running a find and replace on a Word document using VBA. The issue I'm having is that wildcards don't support "0 or 1 occurrences" of a character. The document has tags of the form |tag| and |Endtag|. Depending on the specific project, the content between the tags is either kept or deleted. However, I'd also like the find/replace to delete a newline at the end of |Endtag|, if it exists. The wildcard expression "|" & tagname & "|*|End" & tagname & "|" & vbCr doesn't work, as it explicitly requires a newline to be at the end.

Is there a clean alternative?

Upvotes: 0

Views: 184

Answers (2)

dc-ddfe
dc-ddfe

Reputation: 495

I found a better approach after some experimentation:

'find text without considering newline
Do While Selection.Find.Execute(FindText:=x)
    'if next character after selection is newline:
    If Selection.Next(unit:=wdCharacter) = Chr(13) Then
        'expand selection by one character
        Selection.MoveEnd unit:=wdCharacter, count:=1
    End If
    ran.Delete
Loop

Upvotes: 0

macropod
macropod

Reputation: 13490

Try:

"|" & tagname & "|*|End" & tagname & "[|^13]{1,}"

Upvotes: 1

Related Questions