Mailmerge problem opening datasource

rdatlanta

New Member
Joined
Jun 23, 2017
Messages
1
Hello:

I am trying to do a mail merge using vba, and it keeps failing to connect the datasource. When I run the code (below), it throws up a "select table" message to a table that does not exist. The non-existant table is always in a directory which is one step closer to the root than the directory I'm giving it.

For exampe the code below will open up a "select table" dialog looking for "C:\Users\Roo\My Documents\Properties\CA.xls" (And note: it is looking for a .xls even though I am using and specified .xlsx)

I've tried disconnecting original word document from its data source, I've tried looking at both the word and excel document with document inspector and cleaning out any xml, I've tried creating a new version of the data source with the name that the "select table" dialog is looking for. All to no avail.

Driving me nuts!!

Any suggestions would be appreciated.

Thanks

rdatlanta




Sub MyMailMerge()
Dim DocumentTemplate As String
Dim DataSource As String
Dim DataRow As Integer
Dim NewMergedDocument As String

DocumentTemplate = "C:\Users\Roo\My Documents\Properties\303CA\Welcome Letter.docx"
NewMergedDocument = "C:\Users\Roo\My Documents\Properties\303CA\Merge.docx"
DataSource = "C:\Users\Roo\My Documents\Properties\CA\Clients.xlsx"
DataRow = 443

Call MailMergeTest(DocumentTemplate, DataSource, DataRow, NewMergedDocument)

End Sub
Function MailMergeTest(DocumentTemplate As String, DataSource As String, DataRow As Integer, NewMergedDocument As String)
'test mail merge
Dim oWord As Object
Dim oDoc As Object

Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = -1
Set oDoc = oWord.Documents.Open(DocumentTemplate, ReadOnly:=True)

With oDoc.MailMerge
.OpenDataSource Name:=DataSource, ReadOnly:=True, SQLStatement:="SELECT * FROM `'Bookings$'`"
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = DataRow
.LastRecord = DataRow
End With
.Execute Pause:=False
End With

End Function.


Edit: In case relevant: I'm using Windows 7 Pro, Word and Excel 2010.
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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