Mail Merge Runtime Error

VBA-Idiot

New Member
Joined
Mar 14, 2012
Messages
17
Hi,

I've got a macro which formats a worksheet, opens a pre-formatted word document then runs a mail merge on the worksheet data. Getting a runtime error 4198 and when I run it in the editor it highlights the SQL Statement paragraph and states - Identifier under cursor not recognised.

This is the code which seems to be causing the problem - All i have changed is removing a filepath.

It opens the word document but fails to run the merge.

Appreciate any suggestions!

Code:
'run mail merge
    Dim wd As Object
    Dim wdocSource As Object
    Dim strWorkbookName As String
    Dim NewFileName As String
    Dim wdOutputName, wdInputName As String
    wdOutputName = ThisWorkbook.Path & "\Merged information " & Format(Date, "dd mmm yyyy")
    wdInputName = ThisWorkbook.Path & "\Form.docm"
    
    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("[I]filepath[/I].docm")
    strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
    wdocSource.MailMerge.MainDocumentType = wdformletters
    wdocSource.MailMerge.OpenDataSource _
            Name:=strWorkbookName, _
            AddToRecentFiles:=False, _
            Revert:=False, _
            Format:=wdopenformatAuto, _
            Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
            SQLStatement:="SELECT * FROM `2$`"
    With wdocSource.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With
    wd.Visible = True
    wd.Application.ActiveDocument.SaveAs wdOutputName
   
    wdocSource.Close SaveChanges:=False
    
    Set wdocSource = Nothing
    Set wd = Nothing
Application.DisplayAlerts = False
On Error Resume Next
Sheets("2").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit

Thanks

C
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
did you step thru it (f8) to see where it fails?
does it throw an error?
if so , where?

you need to remove
On Error Resume Next
so we can find the error
 
Upvote 0
I stepped through it it fails at the line
Code:
SQLStatement:="SELECT * FROM `2$`"
The source workbook has a sheet called "2" it should pull the data but that's where it stops.
 
Upvote 0

Forum statistics

Threads
1,216,160
Messages
6,129,215
Members
449,493
Latest member
JablesFTW

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top