Retrieve a line from a Word document

D

Dory550

Hi guys
Can please someone tell me.
How can I extract an email address from line# 17 of Word docoment and store
it in a variable (using vba)?
The email address is the only text on line# 17
Thank you.
Dory
 
D

Doug Robbins - Word MVP

If it was the 17th paragraph, you could use

Dim arange as Range
Set arange = ActiveDocument.Paragraphs(17).Range

then arange will contain the email address.

Otherwise, assuming that line 17 is the only line containing an email
address, Modify the following:



Macro to extract all of the email addresses from a document

Sub CopyAddressesToOtherDoc()


Dim Source As Document, Target As Document, myRange As Range
Set Source = ActiveDocument
Set Target = Documents.Add

Application.ScreenUpdating = False

Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With

Selection.HomeKey Unit:=wdStory
Target.Activate

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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