Problem with execute command in mailmerge

J

JurgenRoeland

Hi all,

I'm using word automation with Ms Office 2003.
I used to have working sources for Ms Office 2000, but as of always, things
evolve...
I recreated my classes in visual c++ 6 based on the MSWORD.OLB file.
Opening word is working again perfectly, opening the datasource as well.
But when I set the destination for the mailmerge object to wdSendToPrinter
as it always was and then perform an execute method, I get an exception
that says 'word could not finish merging these documents or inserting this
database' and nothing gets sent to the printer. However, If I set the
destination to wdSendToNewDocument and execute, the new document is
created. Anybody any idea how I could make it work with sendtoprinter, or
what could be the cause of this error.

Thanks in advance,

J Roeland


_ApplicationWord* objWord = new _ApplicationWord();
if (!objWord->CreateDispatch("Word.Application"))
{
AfxMessageBox("Couldn't get Word object.");
return;
}

objWord->SetVisible(TRUE); //This shows the application.

Documents docs(objWord->GetDocuments());
_Document testDoc;
testDoc.AttachDispatch( docs.Open(
COleVariant(document,VT_BSTR), //FileName
covFalse, // Confirm Conversion.
covFalse, // ReadOnly.
covFalse, // AddToRecentFiles.
covOptional, // PasswordDocument.
covOptional, // PasswordTemplate.
covFalse, // Revert.
covOptional, // WritePasswordDocument.
covOptional, // WritePasswordTemplate.
covOptional, // Format.
covOptional, // Encoding
covOptional, // Visible
covOptional, //
OpenConflictDocument
covOptional, //
OpenAndRepair
covOptional, //
DocumentDirection
covOptional //
NoEncodingDialog
)
);
MailMerge mergemail;
mergemail = testDoc.GetMailMerge();

long values = mergemail.GetState();
if(values != 1)//wdMainAndDataSource
{
CString strConnection = "DSN=MS Access
Databases;DBQ=C:\\ifastoVDB\\facstock2000.mdb;FIL=RedISAM;";

mergemail.OpenDataSource( "c:\\IfastoVDB
\\facstock2000.mdb",
covFalse,
covFalse,
covFalse,
covTrue,
covFalse,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
COleVariant(strConnection),
COleVariant("Select * from
[USERQUERY_MailingKlanten]",VT_BSTR),
USERQUERY_MailingKlanten",VT_BSTR),
covOptional, //COleVariant("Select
from [USERQUERY_MailingKlanten]",VT_BSTR)
covFalse,
covOptional
);
values = mergemail.GetState();
}

mergemail.SetDestination(1);//0=wdSendToNewDocument,1
=wdSendToPrinter,2=wdSendToEmail,3= wdSendToFax

try
{
mergemail.Execute( covOptional);
} catch (CException* exc)
{
AfxMessageBox(exc->ReportError());
}
 
D

Doug Robbins - Word MVP

How about send to the new document and then use code to print that out.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

JurgenRoeland said:
Hi all,

I'm using word automation with Ms Office 2003.
I used to have working sources for Ms Office 2000, but as of always,
things
evolve...
I recreated my classes in visual c++ 6 based on the MSWORD.OLB file.
Opening word is working again perfectly, opening the datasource as well.
But when I set the destination for the mailmerge object to wdSendToPrinter
as it always was and then perform an execute method, I get an exception
that says 'word could not finish merging these documents or inserting this
database' and nothing gets sent to the printer. However, If I set the
destination to wdSendToNewDocument and execute, the new document is
created. Anybody any idea how I could make it work with sendtoprinter, or
what could be the cause of this error.

Thanks in advance,

J Roeland


_ApplicationWord* objWord = new _ApplicationWord();
if (!objWord->CreateDispatch("Word.Application"))
{
AfxMessageBox("Couldn't get Word object.");
return;
}

objWord->SetVisible(TRUE); //This shows the application.

Documents docs(objWord->GetDocuments());
_Document testDoc;
testDoc.AttachDispatch( docs.Open(
COleVariant(document,VT_BSTR), //FileName
covFalse, // Confirm Conversion.
covFalse, // ReadOnly.
covFalse, // AddToRecentFiles.
covOptional, // PasswordDocument.
covOptional, // PasswordTemplate.
covFalse, // Revert.
covOptional, // WritePasswordDocument.
covOptional, // WritePasswordTemplate.
covOptional, // Format.
covOptional, // Encoding
covOptional, // Visible
covOptional, //
OpenConflictDocument
covOptional, //
OpenAndRepair
covOptional, //
DocumentDirection
covOptional //
NoEncodingDialog
)
);
MailMerge mergemail;
mergemail = testDoc.GetMailMerge();

long values = mergemail.GetState();
if(values != 1)//wdMainAndDataSource
{
CString strConnection = "DSN=MS Access
Databases;DBQ=C:\\ifastoVDB\\facstock2000.mdb;FIL=RedISAM;";

mergemail.OpenDataSource( "c:\\IfastoVDB
\\facstock2000.mdb",
covFalse,
covFalse,
covFalse,
covTrue,
covFalse,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
COleVariant(strConnection),
COleVariant("Select * from
[USERQUERY_MailingKlanten]",VT_BSTR),
USERQUERY_MailingKlanten",VT_BSTR),
covOptional, //COleVariant("Select
from [USERQUERY_MailingKlanten]",VT_BSTR)
covFalse,
covOptional
);
values = mergemail.GetState();
}

mergemail.SetDestination(1);//0=wdSendToNewDocument,1
=wdSendToPrinter,2=wdSendToEmail,3= wdSendToFax

try
{
mergemail.Execute( covOptional);
} catch (CException* exc)
{
AfxMessageBox(exc->ReportError());
}
 
C

Candyman

How about send to the new document and then use code to print that out.

Hmm good idea, I hadn't thought of that yet.
Any idea how you can recover the new created document from the documents
collection ?

Jurgen
 
D

Doug Robbins - Word MVP

It will normally be the ActiveDocument.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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