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.
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: