Mail merge problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I try to run the following function I get various error messages or the
system hangs:

Function MergeIt()
Dim objWord As Word.Document
Dim strConnection As String
Set objWord = GetObject("C:\MyMerge.doc", "Word.Document")

strConnection = "Driver={SQL
Server};Server=servername;Database=mydatabasename;Trusted_Connection=yes;"

' Make Word visible.
objWord.Application.Visible = True


' Set the mail merge data source.
objWord.MailMerge.OpenDataSource Name:="c:databasename.adp", _
Connection:=strConnection, _
SQLStatement:="SELECT * FROM tablename"


' Execute the mail merge.
objWord.MailMerge.Execute
End Function


Can anybody tell me where i'm going wrong?
 
Thank you for your reply.

I set the 'Name' to an empty string and got the following error:
Run time error '4198' command failed
 
Sylvain,

Im running office 2003 with sql server 2005

The code I'm running is:

Function MergeIt()
Dim objWord As Word.Document
Dim strConnection As String
Set objWord = GetObject("C:\MyMerge.doc", "Word.Document")

strConnection =

"Driver={SQLServer};Server=SERVERNAME;Database=DATABASENAME;Trusted_Connection=yes;"

' Make Word visible.
objWord.Application.Visible = True


' Set the mail merge data source.
objWord.MailMerge.OpenDataSource Name:="", _
Connection:=strConnection, _
SQLStatement:="SELECT * FROM TABLENAME"


' Execute the mail merge.
objWord.MailMerge.Execute
End Function

It opens the word document ok but fails to merge. I get error code 4198 -
command failed.

Is it a connection string problem?
 
By the way - Im running this code from within an Access project form not Word.
I want my users to to click a button to run mailmerge from within the Access
form environment.

Thanks.
 
Yes, I already know that you are using this from an Access project but this
change nothing to the fact that you making a connection between Word and
SQL-Server. Here, the Access project is out of the equation because you are
using Automation to open Word (but maybe a permission problem for the
secured connection? Did you try with a SQL-Server account and a password?)

I don't use MailMerge, so I cannot tell you more about this problem. You
should ask this in the newsgroup about MailMerge (and make a search with
Google not only for the Web but also for the Groups).

Also, making some tests directly from Word could tell you a lot and don't
forget about the SQL-Server Profiler, too!
 
Back
Top