views:

80

answers:

0

I have created a macro to automate a Word mail merge from excel. Below is my code. The macro is stopping at the highlighted section

Sub RunMerge()

Dim wd As Object

Dim wdocSource As Object

Dim strWorkbookName As String

On Error Resume Next

Set wd = GetObject(, "Word.Application")

If wd Is Nothing Then

Set wd = CreateObject("Word.Application")

End If

On Error GoTo 0

Set wdocSource = wd.Documents.Open("O:\IPSSQL\Master Docs - Aust\Gen_docs\Dividend Priority 549 MERGE DivPrepXLS.doc")

strWorkbookName = ThisWorkbook.FullName

wdocSource.MailMerge.MainDocumentType = wdNotAMergeDocument

wdocSource.MailMerge.MainDocumentType = wdFormLetters

...problem part of code starts here...

wdocSource.MailMerge.OpenDataSource _

Name:=strWorkbookName, _

ConfirmConversions:=False, _

ReadOnly:=False, _

LinkToSource:=True, _

AddToRecentFiles:=False, _

PasswordDocument:="", _

PasswordTemplate:="", _

WritePasswordDocument:="", _

WritePasswordTemplate:="", _

Revert:=False, _

Format:=wdOpenFormatAuto, _

Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _

SQLStatement:="SELECT * FROM '549Merge$'", SQLStatement1:="", SubType _
    :=wdMergeSubTypeAccess

With wdocSource.MailMerge

.Destination = wdSendToNewDocument

.SuppressBlankLines = True

With .DataSource

.FirstRecord = wdDefaultFirstRecord

.LastRecord = wdDefaultLastRecord

End With

.Execute Pause:=False

End With

wd.Visible = True

wdocSource.Close

SaveChanges = False

Set wdocSource = Nothing

Set wd = Nothing

End Sub