'Fill an Outlook email template using words from a Word document using VBA
I am creating a Word document which contains macros that will convert the .doc to .PDF, and include it as an attachment in an autogenerated Outlook email containing the following body:
" Hello (name),
Thank you for contacting us. Please find the attached quotation for: Scope of work: (Scope)"
I would like to replace (name) and (scope) with the customer name and the scope of work entered in the Word document
The information that will be filled by the user in the Word doc to later be included in the email.
What I have so far (the remaining parts are irrelevant to this question):
Private Sub CommandButton1_Click()
Dim strData As String
Dim ola As New Outlook.Application
Dim maiMessage As Outlook.MailItem
Dim fs
myfilename = Split(ActiveDocument.Name, ".")(0)
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
oItem.Display
With oItem
.CC = "[email protected]"
.Subject = "Quote No: " & myfilename
End With
Set objOutlook = Nothing
Solution 1:[1]
The Outlook object model doesn't provide anything for that out of the box. But you can use your own placeholders in the message body (see HTMLBody
) of your template. When a message is created based on the template you can replace such placeholders by replacing custom tags with your own data.
The Outlook object model provides three main ways for working with item bodies:
- Body.
- HTMLBody.
- The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
See Chapter 17: Working with Item Bodies for more information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugene Astafiev |