PDA

View Full Version : HTML format Macro Code for Mail Merge with attachments



MaruVacilo
07-06-2013, 11:12 PM
Hello, I have this code working just perfect.
But the e-mails are without HTML format. How can I do to make them out HTML format? Thanks!



Sub adjuntararchivos()
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Set objEmail = CreateObject("CDO.Message")
objEmail.from = "XXXX@XXXX.com"
Dim bStarted As Boolean
Set oOutlookApp = CreateObject("Outlook.Application")
' Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
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 catalog mailmerge document
With Dialogs(wdDialogFileOpen)
.Show
End With
Set Maillist = ActiveDocument
' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message." ' Set prompt.
title = " Email Subject Input" ' Set title.
' Display message, title
mysubject = InputBox(message, title)
' Iterate through the Sections of the Source document and the rows of the catalog mailmerge document,
' extracting the information to be included in each email.
For j = 1 To Source.Sections.Count - 1
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = mysubject
.Body = Source.Sections(j).Range.Text
Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
Datarange.End = Datarange.End - 1
.To = Datarange
For i = 2 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
Maillist.Close wdDoNotSaveChanges
' Close Outlook if it was started by this macro.
If bStarted Then
oOutlookApp.Quit
End If
MsgBox Source.Sections.Count - 1 & " messages have been sent."
'Clean up
Set oOutlookApp = Nothing
End Sub

Doug Robbins
07-09-2013, 12:18 AM
Download theMergeTools – 20130627 Add-in that I created from the following page of myWindows Live SkyDrive:

https://skydrive.live.com/?cid=5aedcb43615e886b#!/?cid=5AEDCB43615E886B!cid=5AEDCB43615E886B&id=5AEDCB43615E886B%21566 (https://skydrive.live.com/?cid=5aedcb43615e886b#!/?cid=5AEDCB43615E886B!cid=5AEDCB43615E886B&id=5AEDCB43615E886B%21566)

Save the file in the Word Startup folder. In Windows XP the default location for that folder is

C:\Documents and Settings\[User Name]\Application Data\Microsoft\Word\STARTUP

In Windows Vista and Windows 7, it is

C:\Users\[User Name]\AppData\Roaming\Microsoft\Word\STARTUP

When you have done that, a Merge Tools tab will be added to the Ribbon and on that tab there is a Merge with Attachments item that will do what you want.

You may also want todownload the Merging with Attachments document that is also on that page whichexplains how the system is used.

Alternatively, you can look at the code in the Add-in to see how it goes about sending the merged document as the body of an email message in a manner that retains the formatting.