PDA

View Full Version : Solved: Find Pattern Error



gmaxey
10-15-2012, 12:58 PM
I can use VBA to find "(" in a document .Text = "(" and I can used it to find "(" in a document using wildcards .Text = "\(". I can't figure out how to find "(" when using wildcards when I need to group the item: .Text = "(\()"

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "("
If .Execute Then Beep
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "\("
.MatchWildcards = True
If .Execute Then Beep
End With
On Error GoTo err_Pattern
With oRng.Find
.Text = "(\()"
.MatchWildcards = True
If .Execute Then Beep
End With
lbl_Exit:
Exit Sub
err_Pattern:
MsgBox Err.Number & " " & Err.Description & " Why the error?"
End Sub


What I've done is first substitute "(" with "~~" and used .Text = (~~)" then went back and changed "~~" to "(".

Thanks.

macropod
10-15-2012, 03:06 PM
Try:
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "("
If .Execute Then Beep
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\(]"
.MatchWildcards = True
If .Execute Then Beep
End With
On Error GoTo err_Pattern
With oRng.Find
.Text = "([\(])"
.MatchWildcards = True
If .Execute Then Beep
End With
lbl_Exit:
Exit Sub
err_Pattern:
MsgBox Err.Number & " " & Err.Description & " Why the error?"
End Sub

gmaxey
10-15-2012, 04:50 PM
Paul,

Doesn't answer the why, but definately a better how. Thanks.

macropod
10-15-2012, 05:07 PM
I suspect that, when you don't use the '[]', Word isn't recognising the '\' as a special character when used with '(' - it does recognise '\\', though.

FWIW, you could reduce the Find expression to '([(])'.

gmaxey
10-15-2012, 06:49 PM
Paul,

Thanks.