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 / March 2008

Tip: Looking for answers? Try searching our database.

Retrieveing email addresses from Word Doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ray C - 04 Mar 2008 17:28 GMT
I need to extract telephone numbers from a Word document and insert them in
Excel. So I decided to search for the area code (450). However my loop that
executes the Find method doesn't stop, so I'm stuck in a loop.
Can anyone tell me what I'm doing wrong?

For Each rng In objDocument.StoryRanges
           With rng.Find
               .ClearFormatting
               .Text = "450"
               .Wrap = wdFindStop
               .Forward = True
           End With
           Do Until rng.Find.Execute = False
               rng.Expand Unit:=wdSentence
               myArray = Split(rng.Text, " ", -1, vbTextCompare)
               For i = 0 To UBound(myArray)
                   If InStr(1, myArray(i), "450", vbTextCompare) <> 0 Then
                           'Insert into excel cell
                   End If
               Next i
           Loop
       Next rng
Jean-Guy Marcil - 04 Mar 2008 18:26 GMT
> I need to extract telephone numbers from a Word document and insert them in
> Excel. So I decided to search for the area code (450). However my loop that
[quoted text clipped - 18 lines]
>             Loop
>         Next rng

From looking at your code,  I guess the problem is here:

  Do Until rng.Find.Execute = False
  rng.Expand Unit:=wdSentence

You are redefining "rng", so now, the serach is looking at that sentence
over and over.

Work with a duplicate so that you do not touch the original range (the
document range). Try this (untested):

Dim rngStory As Range
Dim objDocument As Document
Dim myArray() As String
Dim i As Long

Set objDocument = ActiveDocument

For Each rngStory In objDocument.StoryRanges
   With rngStory.Find
       .ClearFormatting
       .Text = "450"
       .Wrap = wdFindStop
       .Forward = True
   End With
   Do Until rngStory.Find.Execute = False
       With rngStory.Duplicate
           .Expand Unit:=wdSentence
           myArray = Split(.Text, " ", -1, vbTextCompare)
           For i = 0 To UBound(myArray)
               If InStr(1, myArray(i), "450", vbTextCompare) <> 0 Then
                       'Insert into excel cell
               End If
           Next i
       End With
   Loop
Next

Rate this thread:






 
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.