Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / March 2004

Tip: Looking for answers? Try searching our database.

C# word was unable to open the data source

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Patz - 23 Mar 2004 03:36 GMT
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);
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.