It's a follow up for this question and a print screen embedded there with document view still applies. The code is run from Excel VBA editor.
There is unknown number of email addresses in the Word document and:
I need to extract all of them,
concatenate into one string that contains all of the email addresses, separated using
", "
and fill the string into Excel cell
Activesheet.Range("C31")
Currently I have a code that finds the @
sign and builds email address around that. This is how it looks like:
Sub FindEmail036() '[0-9;A-z;,._-]{1;}\@[0-9;A-z;._-]{1;}
'[0-9;A-z;,._-]{1;}\@[0-9;A-z;._-]{1;}
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim ExcelApp As Excel.Application
Dim rng As Word.Range
Dim emailAdr As String
Dim ws As Worksheet
Dim iCount As Integer
Set WordApp = GetObject(, "Word.Application")
Set ExcelApp = GetObject(, "Excel.Application")
Set WordDoc = WordApp.ActiveDocument
Set rng = WordApp.ActiveDocument.Content
Set ws = ExcelApp.ActiveSheet
ExcelApp.Application.Visible = True
With rng.Find
.Text = "@"
.Wrap = wdFindContinue
.Forward = True
.MatchWildcards = False
.Execute
Debug.Print rng.Text
If .Found = True Then
rng.MoveStartUntil Cset:=" ", Count:=wdBackward
Debug.Print rng.Text
rng.MoveEndUntil Cset:=","
Debug.Print rng.Text
'rng.MoveEndUntil Cset:=" ", Count:=wdBackward
End If
End With 'how to create loop that will extract all the email addresses in the document??
ws.Range("C31").Value = rng
End Sub
This code only extracts the first email address and is not looking for next email addresses. I know this because I'm debugging using F8 and Immediate window and I can see, this code is just finishing search after it finds @
and constructs first complete email address.
I guess some loop is necessary but I don't know how to do write it.
I've also found this source but I don't understand much from it. https://wordmvp.com/FAQs/MacrosVBA/NoTimesTextInDoc.htm