I'm new here so please feel free to point me to another group if this is not
the right one.
I'm using the following VB code to conduct a Mail Merge.
The code is running on XP under MSO 03 (Word VBA6)
This code simply opens the template file (if it is not already open) and
sets up the merge properties and runs the merge.
The extra piece you see, in bold, in the .DataSource.QueryString is my
attempt to get the code to filter the contents of the data file to just those
entries who have 0 for their Owners value.
The part in bold is my problem. It continues to give me a 4198 run-time
error and simply says 'Command Failed'.
Code:
[code]
Sub MergeTest()
Err.Number = 0
On Error GoTo ErrorHandler
Windows("New Ltr-C01.doc").Activate
ErrorHandler:
If Err.Number = 5941 Then
Documents.Open ("X:\Letters\English\Templates\New\New Ltr-C01.doc")
End If
Set letterfile = Documents("New Ltr-C01.doc")
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
sourcefile = "X:Letters\Data\Test Source File.xls"
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User
ID=Admin;Data Source=sourcefile;Mode=Read;Extended
Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDB:"
' connectionstring = "Merge"
' connectionstring = ""
letterfile.CommandBars("Mail Merge").Visible = True
letterfile.MailMerge.MainDocumentType = wdFormLetters
letterfile.MailMerge.OpenDataSource Name:=sourcefile, _
ConfirmConversions:=False, _
ReadOnly:=False, _
LinkToSource:=True, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:=connectionstring, _
SQLStatement:="SELECT * FROM `Merge$`", _
SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
[b].DataSource.QueryString = "SELECT * FROM `Merge$` WHERE
`Owners`=`0`" & ""[/b]
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End Sub
[/code]
I have tried a variety of ways to state the part in bold:
Code:
[code]
..DataSource.QueryString = "SELECT * FROM `Merge$` WHERE Owners=0"
..DataSource.QueryString = "SELECT * FROM Merge WHERE `Owners`=`0`" & ""
..DataSource.QueryString = "SELECT * FROM `Merge` WHERE `Owners`=`0`" & ""
..DataSource.QueryString = "SELECT * FROM `Merge$` WHERE ((Owners=0))" & ""
[/code]
What I've found on the internet has been nice to read but has not solved my
problem.
So far, nothing is working. Anyone have any ideas?
Thanks! JT
Archived from group: microsoft>public>vb>ole