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.

retrieving email address surrounded by tabs, carriage returns, etc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ray C - 06 Mar 2008 22:48 GMT
I'm using Word VBA to retrieve email addresses inside a Word document. The
problem I encounter is that sometimes there are tabs, carriage returns, ie
special characters before and after the email address that also get pulled
and appear as small squares in the text that I retrieve. My logic is like
this:

1) Find occurence of @ sign.
2) Pull the sentence that contains the @ sign and use the Split function to
create an array of words in the sentence (one word will eventually contain
the full email address).

Problem: When I look at the array item that contains the email address,
there are also tabs, carriage returns, etc that get interpreted as small
squares in my output.

Here is my code:

For Each rngStory In objDocument.StoryRanges
           With rngStory.Find
               .ClearFormatting
               .Text = "@"
               .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), "@", vbTextCompare) <> 0 Then
                           If numEmailsFound < 3 Then
                               '/// Copy email address to excel.
                           End If
                       End If
                   Next i
               End With
           Loop
       Next rngStory
Graham Mayor - 07 Mar 2008 06:51 GMT
Try using Word to determine what is an e-mail address by using autoformat eg

Sub CopyEMailAddressesToOtherDoc()
Dim Source As Document
Dim Target As Document
Dim myRange As Range
Dim sView As String

Set Source = ActiveDocument
sView = ActiveWindow.View.ShowFieldCodes
Set Target = Documents.Add
Application.ScreenUpdating = False
Source.Activate
Options.AutoFormatReplaceHyperlinks = True
With Selection
   .Range.AutoFormat
ActiveWindow.View.ShowFieldCodes = True
   .HomeKey unit:=wdStory
   With .Find
       .ClearFormatting
       .Replacement.ClearFormatting
       Do While .Execute(findText:="^d HYPERLINK ""Mailto", _
           MatchWildcards:=False, Wrap:=wdFindStop, _
           Forward:=True) = True
           Set myRange = Selection.Range
           Target.Range.InsertAfter myRange & vbCr
       Loop
   End With
   .HomeKey unit:=wdStory
End With
ActiveWindow.View.ShowFieldCodes = sView
Source.Close Savechanges:=wdDoNotSaveChanges
Target.Activate
With Selection.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Text = "^019 HYPERLINK ""mailto:(*)"" ^21"
   .Replacement.Text = "\1"
   .MatchWildcards = True
   .Execute Replace:=wdReplaceAll
End With
Selection.Sort FieldNumber:="Paragraphs", _
    SortFieldType:=wdSortFieldAlphanumeric, _
    SortOrder:=wdSortOrderAscending
End Sub

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> I'm using Word VBA to retrieve email addresses inside a Word
> document. The problem I encounter is that sometimes there are tabs,
[quoted text clipped - 34 lines]
>            Loop
>        Next rngStory
Tony Jollans - 07 Mar 2008 12:42 GMT
Much easier just to walk the hyperlinks ...

   Dim HLink As Hyperlink
   Dim Source As Document
   Dim Target As Document
   Dim E_Mail() As String

   Set Source = ActiveDocument
   Set Target = Documents.Add

   For Each HLink In Source.Hyperlinks
       E_Mail = Split(HLink.Address, ":", 2)
       If E_Mail(0) = "mailto" Then
           Target.Content.InsertAfter E_Mail(1)
           Target.Range.InsertParagraphAfter
       End If
   Next

You can tidy it up to get exactly what you want

Signature

Enjoy,
Tony

> Try using Word to determine what is an e-mail address by using autoformat
> eg
[quoted text clipped - 81 lines]
>>            Loop
>>        Next rngStory
Ray C - 07 Mar 2008 20:44 GMT
Tony, unfortunately your code doesn't work for all my documents because Word
does not recognize all of the email addresses as hyperlinks. I need to go
into each document, put my cursor at the end of the email address and press
Enter. At that point Word changes it into a hyperlink (blue color).

But I have over 5000 documents to process so your code won't work. I need a
more solid approach, i.e. search for @ sign and work from there.

Thanks

> Much easier just to walk the hyperlinks ...
>
[quoted text clipped - 101 lines]
> >>            Loop
> >>        Next rngStory
Ray C - 07 Mar 2008 20:50 GMT
Hi Tony,

I got it to work...
I added two lines from Graham's reply to the beginning of your code and it
worked.

objWord.Options.AutoFormatReplaceHyperlinks = True
objDocument.Range.AutoFormat

Thanks!

> Much easier just to walk the hyperlinks ...
>
[quoted text clipped - 101 lines]
> >>            Loop
> >>        Next rngStory
Tony Jollans - 09 Mar 2008 14:05 GMT
Well done! Making all the addresses hyperlinks first does help in finding
them within the hyperlinks collection!

Signature

Enjoy,
Tony

> Hi Tony,
>
[quoted text clipped - 113 lines]
>> >>            Loop
>> >>        Next rngStory
 
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.