Excel VBA to Generate Outlook Message with a Body from Word File

Magdoulin

Board Regular
Joined
Jan 11, 2013
Messages
73
Hi Guys,


I am trying to write an VBA Code that would help creating Outlook Email message with To, CC and Subject fields.

However, the body, I need to be read from Word File saved in my computer drive.

Here is what I have reached so far, yet, it works only for creating the email message with the needed field except for the body part.


Sub Email_Template1()

Dim oMailItem As Object
Dim oOLapp As Object
Dim Word As Object
Dim doc As Object
Dim MsgTxt As String

Set oOLapp = CreateObject("Outlook.Application")
Set oMailItem = oOLapp.CreateItem(0)

With oMailItem
.To = "Email"
.CC = "Email"
.Subject = "Needed Confirmation"
.Display
End With

Set Word = CreateObject("word.application")
Set doc = Word.Documents.Open _
(FileName:="G:\Customer Services\File Plan\Magdoulin\To be deleted 1.docx", ReadOnly:=True)
'Pulls text from file for message body
MsgTxt = doc.Range(Start:=doc.Paragraphs(1).Range.Start, _
End:=doc.Paragraphs(doc.Paragraphs.Count).Range.End)

Set oOLapp = Nothing
Set oMailItem = Nothing
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Cross posted https://www.ozgrid.com/forum/forum/...te-outlook-message-with-a-body-from-word-file

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
This worked for me:

Code:
Sub Email_Template1()
Dim oMailItem As Object, oOLapp As Object
Dim Word As Object, doc As Object, MsgTxt$
Set oOLapp = CreateObject("Outlook.Application")
Set oMailItem = oOLapp.CreateItem(0)
Set Word = CreateObject("word.application")
Set doc = Word.Documents.Open(Filename:="c:\pub\first2.docm", ReadOnly:=True)
MsgTxt = doc.Range(Start:=doc.Paragraphs(1).Range.Start, _
End:=doc.Paragraphs(doc.Paragraphs.Count).Range.End)
With oMailItem
    .To = "Email"
    .cc = "Email"
    .Subject = "Needed Confirmation"
    .HTMLBody = MsgTxt
    .Display
End With
Set oOLapp = Nothing
Set oMailItem = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top