Hi Everyone, I have had a problem trying to send a email merge with attachments.

I am following this tutorial:

Mail Merge to E-mail with Attachments

Article contributed by Doug Robbins

This procedure can be used to mail merge to e-mail, including attachments with each message when Microsoft Office Outlook is installed. It has been used with all versions of Office from Office 97 up to and including Office 2007. It is not necessary for Outlook to be nominated as the default mail program, but it must be installed on the system. The procedure can handle multiple attachments for each message, individual attachments for each recipient, common attachments for all recipients, or a mixture of both.

Preparations

To run the macro in this procedure it is necessary to set a reference to the Microsoft Office Outlook Object Library. You do this from within the Visual Basic Editor, by selecting References from the Tool menu and then checking the item Microsoft Office Outlook ##.0 Object Library (where ## is the Outlook version number).

In addition, running the macro will cause the following warning message to be displayed by Outlook for each email message that the macro sends:
Warning that other program is attempting to send an email



You can avoid this happening by downloading the "Express ClickYes" utility that is available as a free download from:

http://www.contextmagic.com/express-clickyes/

Express ClickYes is a tiny program that sits in the taskbar and clicks the Yes button on behalf of you, when Outlook's Security Guard opens prompt dialog saying that a program is trying to send an email with Outlook or access its address book. You can suspend/resume it by double-clicking its taskbar icon. Developers can automate its behaviour by sending special messages.

Setup

You will need to create a separate Catalog (or in Word 2002 and later, Directory) type mail merge main document which creates a word document containing a table in each row of which would be data from the data source that contains the email address in the first column and the Drive:\Path\Filename of each attachment in the second and any subsequent columns, one attachment per cell.

The data source for this Catalog/Directory type mail merge must be the same data source as that which you have, or will use for creating the mail merge that you want to email with attachments.

I cannot emphasise too strongly that the path and filenames for the attachments must be IDENTICAL to the actual path and filenames. If they are not, the files will not be attached. A single space in the wrong place is all that it will take for the procedure to fail.

In the Catalog/Directory type mail merge main document, insert a one row table into the cells of which you insert the relevant merge fields from the data source, similar to the following:

<<EmailAddress>>


<<Attachment1>>


<<Attachment2>>

If the same attachment(s) are to be sent to all of the recipients, and they are not included in the datasource, the Drive\Path\Filename of the attachment(s) can be typed directly in the cell(s) of the Catalogue/Directory mail merge main document in place of the <<Attachment>> fields. In this case, your main document would look like:

<<EmailAddress>>


D:\Documents\JulyReport.doc

You can also have a mixture of a standard attachment for each recipient and an individual one by setting the main document up in the following fashion:

<<EmailAddress>>


D:\Documents\JulyReport.doc


<<Attachment2>>

After creating this Catalog/Directory mail merge main document, execute the mail merge to a new document, which if you have set it up correctly will produce a document containing a table similar to the following with as many rows as there are records in the data source:

[email protected]


D:\mugshots\billsmith.jpg


D:\resumes\billsmith.doc

[email protected]


D:\mugshots\jowblow.jpg


D:\resumes\joeblow.doc

Or in the case of a common attachment for each recipient:

[email protected]


D:\Documents\JulyReport.doc

[email protected]


D:\Documents\JulyReport.doc

Save that file and close it. When you run the macro that is provided below, you will be asked to open that document, so remember the name that you have given to it and where you saved it.

Then execute to a new document the mail merge that you want to send out by email with the attachments and with the result of the execution of that mail merge on the screen, run a macro containing the following code. (This code is designed to be pasted into the VBA Editor: it is much easier to read if you do that!)

Sub emailmergewithattachments()

Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Dim bStarted As Boolean
Dim oOutlookApp As 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




I have done everything that the tutorial asks, I have set a reference to the Microsoft Office Outlook Object Library. I have created a mail merge document and changed it to a directory type using a table as displayed in the tutorial. I exported the file to a new document and it created the email addresses in the table formats with the email I want to send how it says it should in the tutorial.

The first file was called email merge doc 2 and the second file was called directory 1.

I closed directory 1 as described in the tutorial and then ran the macro from the vb editor in the file email merge doc 2. The macro runs correctly with no errors but at the end a messae box appears saying '0 emails have been sent'

There is one thing that I think might have something to do with it although I am unsure why it would affect the process.

When I close email merge doc 2 then reopen it, it says that the document contains information connected to an sql database, because I got the emails from a business directory database programme, but i cannot reconnet to the programme so have to remove the merge info and then repeat it once the document has been reopened. Although I do not think this would affect it because I have still created the directory with all the email address and attachments in the format that the tutorial requires, I just thought I would mention it to give some more background information on the issue and what I am trying to do.

Does anyone know why I might be having these problems?

Thank you very much in advance for any answers!