Excel VBA Build Outlook Body from Word Document

Hi

What Excel VBA code would I use to loop through the content of a Word document
and build the body of an Outlook email where the word  "Today" in Word is replaced by today's date?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveCost AccountantCommented:
You would use MailMerge in Word.
MailMerge
Video

This is the system in Word specifically there for what you need, without any need for VBA.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I specifically said asked for Excel VBA code to loop through a Word document and build the body of a on Outlook email
SteveCost AccountantCommented:
OK, so lets get a bit more meat on the bones :)

You would like to:

Run code in excel to:
1) Open a word document
2) Read the text in the word document
3) Replace "Today" with Today()
4) Then use the text from the word document as the body of the mail.

Will there be many word documents?
Do you loop through a list in Excel (with mail addresses etc) for a single document?
Do you need to maintain formatting of the body of text (e.g fonts bolds underlines)?
Will there be a need to have attachements?
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

tilsantCommented:
Probably do a find-replace in word:
With Selection
    .Find.ClearFormatting
    .Find.Text = "Today"
    .Find.Replacement.Text = Format(date, "dd-mmm-yy")
    .Find.Forward = True
    .Find.Wrap = wdFindContinue
    .Find.Format = False
    .Find.MatchCase = False
    .Find.MatchWholeWord = False
    .Find.MatchWildcards = False
    .Find.MatchSoundsLike = False
    .Find.MatchAllWordForms = False
    .Find.Execute Replace:=wdReplaceAll
End With

Open in new window


You may change the date format as required, in row 4.

- tilsant
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Steve
Yes that is exactly what I need (Points 1 to 4)
Then to answer your questions:
Will there be many word documents? No
 Do you loop through a list in Excel (with mail addresses etc) for a single document? Yes exactly
 Do you need to maintain formatting of the body of text (e.g fonts bolds underlines)? Not really but would be nice
 Will there be a need to have attachements? No

Thanks very much
Murray
SteveCost AccountantCommented:
Would you like to 'pick' the Word Document using FilePicker or have it hard coded to the VBA?
File Picker is easier to select the file (in case it changes).

Do you have a sample of the Excel document and the Word document (or simple mock ups).
This will ensure we get the right layouts and result for you.

ATB
Steve.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi I would rather have it hard coded in the VBA in this instance as it won't change. I have no samples.
Thanks
Rob HensonFinance AnalystCommented:
If the word document has an autofield for current date rather than "Today", would that not convert automatically anyway???
SteveCost AccountantCommented:
OK... here is a simple set of code for:

1) Open workbook and copy the test into 'body'
2) Replace [TODAY] with todays Date() Formatted.
3) Create a mail and using the list of addresses in the file.

Sub NewWordDocument()

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim OutApp As Object
Dim OutMail As Object
Dim i As Integer

myFname = "U:\Test\TestDOC.docx"

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open(myFname, , True)
body = wrdDoc.Content
wrdDoc.Close ' close the document
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing

body = Replace(body, "[TODAY]", Format(Now(), "dddd dd mmm yyyy"))

For i = 2 To Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

mAddress = Sheets("Sheet1").Range("A" & i)
mSubject = Sheets("Sheet1").Range("C" & i)

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .to = mAddress
        .CC = ""
        .BCC = ""
        .Subject = mSubject
        .body = body
        .Send
    End With

Next i

End Sub

Open in new window


You will need to change this line of code:
myFname = "U:\Test\TestDOC.docx"
To the file location and namer of the Word Document.

I have used [TODAY] in the word document as it is easier to see in the file and less likely to replace something you may need left alone.
I have allowed the subject to be entered into the excel file. this is just to demonstrate how to change the subject.

Note that the formatting of the body is not maintained.
U--Test-TestXLSM.xlsm
U--Test-TestDOC.docx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much. Great answer. I appreciate the time taken.
Murray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.