How do I find a pattern within Word/

O

Oozle_Finch

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
 
G

Greg Maxey

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
 
K

Klaus Linke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top