MailMerge with SQL Server, OpenDataSource problem

T

tyler

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
 
C

Cindy M -WordMVP-

Hi Tyler,

Are you still looking for help with this? If you are, which version of
Word are you targetting for automation?

Word95 is a totally different world compared with Word 97 and later.
Word95 used WordBasic; later versions use VBA. I can't tell from your
code how you've tried to link up to Word...
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.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top