Hello,
I am trying to efficiently/quickly(in terms of CPU usage and time) find a
pattern of text within a Word document. Basically, 3 digits followed by a
dash, and then 4 more digits. I am wondering if the below code is appropriate?
Application.Selection.HomeKey( _
Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
Dim strFind As String = ([0-9]{3})-([0-9]{4})
Dim fnd As Word.Find = Application.Selection.Find
fnd.ClearFormatting()
fnd.Text = strFind
If fnd.Execute() Then
MessageBox.Show("Text found.")
Else
MessageBox.Show("Text not found.")
End If
Thanks for any help :-)
Jeremy
Greg Maxey - 23 Feb 2007 11:44 GMT
I would say no since it doesn't work for me. If it works for you that is a
different story. I would do it like this:
Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
With oRng.Find
.Text = "[0-9]{3}-[0-9]{4}"
.MatchWildcards = True
If .Execute Then
MsgBox "Text found."
Else
MsgBox "Text not found."
End If
End With
End Sub

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> Hello,
>
[quoted text clipped - 18 lines]
> Thanks for any help :-)
> Jeremy
Klaus Linke - 28 Feb 2007 05:11 GMT
Hi Jeremy,
If it works, it's appropriate ;-)
You wouldn't need wildcards, necessarily.
^#^#^#-^#^#^#^# without wildcards should work, too.
You'd have to test what solution is faster, if speed is an issue at all.
Regards,
Klaus
> Hello,
>
[quoted text clipped - 18 lines]
> Thanks for any help :-)
> Jeremy