Hello all! This is my first time posting here.
I am trying to use a macro to run a MailMerge that includes:
- Recipients in the Cc list
- A personalised attachment for each different email
- An email message that contains:
- Hyperlinks
- Lists
- A table
- A picture
I have managed to get almost everything working by analysing the solution provided by Paul (macropod) in another thread
here. The only problem that I have not been able to resolve is that the email body comes out as just text. All formatting, the table and lists structures, and the picture are not in the emails generated.
Below is a preview of how the email
should have looked like:
Below is the code that I used:
Code:
Option Explicit
Sub MailMergeWithCcAndAttachments()
Dim Source As Document, MailList As Document, TempDoc As Document
Dim Datarange As Range, Recipient As Range, CC1 As Range, CC2 As Range
Dim i As Long, j As Long, bStarted As Boolean, objDoc, objSel
Dim oOutlookApp As Outlook.Application, oItem As Outlook.MailItem
Dim MySubject As String, Message As String, Title As String
' Run the MailMerge to Letters
ActiveDocument.MailMerge.Execute
Set Source = ActiveDocument
' CHeck if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err = 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
' Open the MailMerge Catalog/Directory document (MailingList.docx)
Dialogs(wdDialogFileOpen).Show
Set MailList = ActiveDocument
' Show an input box asking the user for the email subject to be used.
' Display message of input box, title
MySubject = InputBox("Enter the subject to be used for each email.", "Email Subject Input")
' Iterate through the Sections of the Source document and the rows of the MailMerge Catalog/Directory document,
' extracting the information to be included in each email.
For j = 1 To Source.Sections.Count - 1
Source.Sections(j).Range.Copy
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = MySubject
.BodyFormat = olFormatHTML
.Body = Source.Sections(j).Range.FormattedText
.Display
Set objDoc = .GetInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Paste
Set Recipient = MailList.Tables(1).Cell(j, 1).Range
Recipient.End = Recipient.End - 1
Set CC1 = MailList.Tables(1).Cell(j, 2).Range
CC1.End = CC1.End - 1
Set CC2 = MailList.Tables(1).Cell(j, 3).Range
CC2.End = CC2.End - 1
.To = Recipient.Text
.CC = CC1.Text & "; " & CC2.Text
' To obtain the file path for the attachment from the MailMerge Catalog/Directory document.
For i = 4 To MailList.Tables(1).Columns.Count
Set Datarange = MailList.Tables(1).Cell(j, i).Range
Datarange.End = Datarange.End - 1
.Attachments.Add Trim(Datarange.Text), olByValue, 1
Next i
.Send
End With
Set oItem = Nothing
Next j
Source.Close wdDoNotSaveChanges
MailList.Close wdDoNotSaveChanges
' Close Outlook if it was started by this macro.
If bStarted Then oOutlookApp.Quit
MsgBox Source.Sections.Count - 1 & " message(s) have been sent."
'Clean up
Set Recipient = Nothing: Set oOutlookApp = Nothing: Set Source = Nothing
Set oItem = Nothing: Set objDoc = Nothing: Set objSel = Nothing
End Sub
It should be noted that the intended text and formatting are actually copied to the clipboard, as a manual paste in a new email places everything in order. However, this is just not coming through for the ones generated by the macro.
I am a complete novice at VBA (picked up this entirely from the forum resources), so any help would be greatly appreciated. It would allow me to adapt this macro for other similar emails in the future.
Not sure if anything from another
forum post might help.
-----------------------------------------------------
For those equally new to VBA, I understand the logic gaps faced while trying to find a solution to my problem. Below are some detailed steps that would have made my journey easier.
Setting up the files for the uninitiated
You must have Outlook Desktop installed.
1. Open the "
Notification Email.docm" file, and start the Step-by-Step Mail Merge Wizard. Choose the "Letters" document type.
2. For the recipients, use the "
MailingList.xlsx" file.
3. You do not need to complete the merge.
4. Open the "
MailingList.xlsx" file, and go to Column E (Attachment).
5. You can replace the values in Cells E2 and E3 with any 2 working file directories for testing.
6. After you have put in working directory lists, copy cells B2 to E3 only (without the headers).
6. Open the "
MailingList.docx" file, and replace the entire table there with your updated table. Save the file.
7. Go back to the "
Notification Email.docm" file, and go to the Developer tab (you might have to add it to your Ribbon via File > Options).
8. Select "Visual Basic" > Tools > References.
9. Ensure that the Microsoft Outlook Object Library is enabled.
10. Click "OK" and close the Visual Basic window.
11. Select "Macros" > "MailMergeWithCcAndAttachments" > Run
- If you do not see it there, then you might have to create a new macro with the code above.
12. When a dialogue box appears, select the "MailingList.docx" file (NOT the Excel file).
13. Enter a subject for the emails to be created.
14. The macro should run and create 2 new emails in Outlook.