Its been 3.5 years since my last ASP VB mail merge of Word with an Access document
Set wdDoc = wdApp.Documents.Open("C:\InetPub\Test\_docs\MyFile.doc"
wdDoc.MailMerge.MainDocumentType = wdFormLetter
wdDoc.MailMerge.OpenDataSource ,,,,,,,,,,,"DSN=MyTest","SELECT * FROM MyTable
Now I want to do a C# mail merge of Word with a SQL database
I done some research and seen several posts that indicate this can be tricky
I don't mind using a DSN so long as it works without poping up a user interface/input window
My latest attempt is to use the following OpenDataSource, but they have all failed
Do you see a mistake that is keeping this from working
object missingValue = Type.Missing
object connection = "Provider=SQLOLEDB.1;User ID=MyUid;Password=MyPwd;Persist Security Info=True;Initial Catalog=MyDatabase;DataSource=MySqlServer;"
object sql = "SELECT * FROM MyTable WHERE MyId=7"
object subtype = Word.WdMergeSubType.wdMergeSubTypeWord2000
Word_App.ActiveDocument.MailMerge.OpenDataSource("", ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref connection, ref sql, ref missingValue, ref missingValue, ref subtype);
Peter Jamieson - 24 Mar 2004 11:07 GMT
> I done some research and seen several posts that indicate this can be tricky.
> I don't mind using a DSN so long as it works without poping up a user interface/input window.
> My latest attempt is to use the following OpenDataSource, but they have all failed.
>
> Do you see a mistake that is keeping this from working?
At the moment you are specifying an OLEDB provider in your DSN. The only way
you will be able to make this work with SQL Server (which is what you appear
to be using) is to create a .odc file and specify that in the Name parameter
of the OpenDataSource call. You can set up the .odc file with all the
necessary connection info. and use that. In previous experiments I've also
been able to create a completely blank .odc file and supply all the
connection info. in the connection parameter, but that may have changed in
Word 2003. In this case you will also need /not/ to specify the
wdMergeSubTypeWord2000 subtype.
Other possible approaches are:
a. set up the connection info. using the data link editor (i.e. create a
.udl file) anbd specify that as the Name. That also connects via OLEDB. It
won't work with Word 2000.
b. Use ODBC to connect. In that case you need an ODBC DSN (any type will
do) and an /ODBC/ connection string, which is different from an OLEDB
connection string. In all cases I think you need the wdMergeSubTypeWord2000
subtype.
If you have a User/System DSN you provide a blank Name parameter, start the
Connection parameter with
DSN=yourdsnname;
If you have a file DSN, put the .dsn file's pathname in the Name parameter
and start the Connection parameter with
FILEDSN=yourfiledsnpathname;
You may also have to play around with the syntax of your SQL statement -
something (I think it is the OLEDB provider but I do not know) is more
finickity than it should be.

Signature
Peter Jamieson - Word MVP
Word MVP web site http://word.mvps.org/
> Its been 3.5 years since my last ASP VB mail merge of Word with an Access document.
> Set wdDoc = wdApp.Documents.Open("C:\InetPub\Test\_docs\MyFile.doc")
[quoted text clipped - 11 lines]
>
> object connection = "Provider=SQLOLEDB.1;User ID=MyUid;Password=MyPwd;Persist Security Info=True;Initial
Catalog=MyDatabase;DataSource=MySqlServer;";
> object sql = "SELECT * FROM MyTable WHERE MyId=7";
>
> object subtype = Word.WdMergeSubType.wdMergeSubTypeWord2000;
>
> Word_App.ActiveDocument.MailMerge.OpenDataSource("", ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue, ref
missingValue, ref connection, ref sql, ref missingValue, ref missingValue,
ref subtype);