Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

How do I find a pattern within Word/

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oozle_Finch - 23 Feb 2007 06:35 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.