Word Automation Preview

R

Ryan Wade

When we have fill-in fields in our docs, upon previewing them with MFC
Automation, the toolbars and menu are invisible. If we preview a doc with
no fill-in fields it is OK. Any thoughts? Code is below.

int _stdcall WordPreview(LPSTR docFileName, LPSTR dataFileName)
{

if(!docFileName || (strlen(docFileName) == 0)){
SetLastError("No doc file specified");
return 0;
}
if(!dataFileName || (strlen(dataFileName) == 0)){
SetLastError("No data file specified");
return 0;
}

ResetLastError();

_Application oApp;
_Document oDoc;
Documents oDocs;
COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR),
vTrue((short)TRUE),
vFalse((short)FALSE);
_Document ResultName;

//Start Word.
if(!oApp.CreateDispatch("Word.Application",NULL))
{
SetLastError("Unable to start Word.");
return 0;
}

oDocs = oApp.GetDocuments();

try
{
//Word9
#ifdef __WORDVER9__
oDoc = oDocs.Open(COleVariant(docFileName), vOpt,
COleVariant(vFalse) ,vOpt , vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
#endif

//Word10
#ifdef __WORDVER10__
oDoc = oDocs.Open(COleVariant(docFileName), vOpt, COleVariant(vFalse)
,vOpt , vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
#endif

//Word11
#ifdef __WORDVER11__
oDoc = oDocs.Open(COleVariant(docFileName), vOpt, COleVariant(vFalse)
,vOpt , vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt);
#endif
}
catch(COleDispatchException *e)
{
SetLastError(e->m_strDescription);
oDoc.Close(vFalse, vOpt, vOpt);
oDoc.ReleaseDispatch();
oDocs.ReleaseDispatch();
oApp.Quit(vOpt,vOpt,vOpt);
oApp.ReleaseDispatch();
return 0;
}

MailMerge oMerge = oDoc.GetMailMerge();

//Set up the mail merge type as form letter and specify the data source.
oMerge.SetMainDocumentType(0L); //1=wdMailingLabels

try
{
//Word9
#ifdef __WORDVER9__
oMerge.OpenDataSource(dataFileName, vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
#endif

//Word10
#ifdef __WORDVER10__
oMerge.OpenDataSource(dataFileName, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
#endif

//Word11
#ifdef __WORDVER11__
oMerge.OpenDataSource(dataFileName, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
#endif
}
catch(COleDispatchException *e)
{
SetLastError(e->m_strDescription);
oMerge.ReleaseDispatch();
oDoc.Close(vFalse, vOpt, vOpt);
oDoc.ReleaseDispatch();
oDocs.ReleaseDispatch();
oApp.Quit(vOpt,vOpt,vOpt);
oApp.ReleaseDispatch();
return 0;
}

//Execute the mail merge.
oMerge.SetDestination(0); //wdSendToNewDocument=0

try
{
oMerge.Execute(vOpt);

}
catch(COleDispatchException *e)
{
SetLastError(e->m_strDescription);
oMerge.ReleaseDispatch();
oDoc.Close(vFalse, vOpt, vOpt);
oDoc.ReleaseDispatch();
oDocs.ReleaseDispatch();
oApp.Quit(vOpt,vOpt,vOpt);
oApp.ReleaseDispatch();
return 0;
}

ResultName = oApp.GetActiveDocument();

oMerge.ReleaseDispatch();


oDoc.Close(vFalse, vOpt, vOpt);

oDoc.ReleaseDispatch();
oApp.SetVisible(TRUE);
oApp.Activate();
oDocs.ReleaseDispatch();
ResultName.ReleaseDispatch();
oApp.ReleaseDispatch();

return 1;
}
//-------------------------------------------------------------
 

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