MailMerge with SQL Server, OpenDataSource problem

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: tyler (tgaede_at_yahoo.com)
Date: 08/03/04


Date: 3 Aug 2004 11:20:04 -0700

I'm trying to do a mail merge in C++ grabbing data from an SQL Server.
I'm not able to get the OpenDataSource() function to work right. I've
setup a wrapper class using OLE Automation and the classwizard and all
that. Currently i'm using the Word95 Type Library (not sure if i can
switch to a newer one) - wb70en32.tlb. I've seen people on this board
using different items for that first parameter, but none have seemed
to work for me. maybe i'm doing something else wrong?

Here is some example code below for what i'm doing. it's not the exact
code, but it's close enough.

///////////////////////Code

_variant_t vSQLStatement;
_variant_t vConnection;
_variant_t vName;
_variant_t v;
_variant_t v1;
_variant_t v0;

vSQLStatement.vt = VT_BSTR;
vSQLStatement.bstrVal = "SELECT Customers.* FROM Customers WHERE
Balance > 500;";

vConnection.vt = VT_BSTR;
vConnection.bstrVal = "Provider=SQLOLEDB;Data
Source=192.168.2.2;Initial Catalog=TRS;User Id=user;Password=pass;";

v.vt = VT_ERROR;
v.scode = DISP_E_PARAMNOTFOUND;

v1.vt = VT_I2;
v1.iVal = 1;

v0.vt = VT_I2;
v0.iVal = 0;

//I don't know what to use here. VT_NULL doesn't work, nor does the
//VT_ERROR. Since the type is text, i think it has to be BSTR. I've
//Also tried pointing to an .odc file, but then it just hangs. I can
//get it to work successfully within Word.
vName.vt = VT_BSTR;
vName.bstrVal = "Whatever";

MailMergeOpenDataSource(vName, v0, v0, v0, v0, v, v, v0, v, v,
vConnection, vSQLStatement, v); //Hangs or fails here.

//////////////////////End code

Here is the documentation i've found for the function. Note that
"Name" is the only required parameter:

MailMergeOpenDataSource Name = text [ConfirmConversions = number]
[ReadOnly = number] [LinkToSource = number] [AddToMru = number]
[PasswordDoc = text] [PasswordDot = text] [Revert = number]
[WritePasswordDoc = text] [WritePasswordDot = text] [Connection =
text] [SQLStatement = text] [SQLStatement1 = text]

Attaches the specified data source to the active document, which
becomes a main document if it is not one already.

Name = The name of the data source. Note that you can specify a
Microsoft Query (.QRY) file instead of a specifying a data source, a
connection string, and a query string.

Thanks in advance!

Tyler



Relevant Pages

  • Re: Word cannot find data source problem
    ... The string will be saved either in the connection string, or in the SQLStatement, or split across the two, depending on how Word chose to make the connection. ... Can you tell me where the path to the data source is saved when I do the ... location of a data source into a "database" and a "table". ...
    (microsoft.public.word.mailmerge.fields)
  • Re: OpenDataSource in vba creates an error
    ... if all the connection information is in the Connection string, ... if you are using Integrated Security for SQL Server access, ... > I have a problem connecting to data source in mail merge. ... > I created a datasource through the Select Data Source screen to connect to ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Word cannot find data source problem
    ... "Peter Jamieson" wrote: ... could you tell me if any data source information is stored in my ... >> the database part of the data source's locatoin in a /Connection>> string/, ... Word constructs a connection string ...
    (microsoft.public.word.mailmerge.fields)
  • Oracle Connection From FrontPage 2003
    ... I am trying to create an Oracle Data source withing ... FrontPage 2003 data source catalog. ... the same connection string: ...
    (microsoft.public.sharepoint.portalserver)
  • Re: Mail Merge with external data source
    ... > Microsoft Query via ODBC, if you could ever figure out the syntax. ... We had that conversation back in 2006 about Word 2004 ODBC connectivity. ... I think by that time several people had probably tried quite hard to get an ODBC connection to work - the only response from MSFT was that the "\c ODBC switch was not supported in the DATABASE field in Word 2004" I think he meant ODBC connectivity wasn't supported, and I would guess it still isn't. ... A data source for a mail merge operation can be an Excel sheet, ...
    (microsoft.public.mac.office.word)