//Commonly used variants.
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//Start a new instance of Microsoft Word
_Application oWordApp;
if (!oWordApp.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.",
MB_OK | MB_SETFOREGROUND);
return;
}
try
{
//Create a new document
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt);
//Add text to the document
Selection oSel;
oSel = oWordApp.GetSelection();
oSel.TypeText("one");
oSel.TypeParagraph();
oSel.TypeText("two");
oSel.TypeParagraph();
oSel.TypeText("three");
//Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oWordApp.SetVisible(TRUE);
// TRY THIS:
// To see the error handling in action, change
// the following line to:
//
// oActiveDoc.SaveAs(COleVariant((long)5),
// COleVariant((short)0),
// vFalse, COleVariant(""), vTrue, COleVariant(""),
// vFalse, vFalse, vFalse, vFalse, vFalse);
//
// ALSO TRY THIS:
// To see the error handling in action, change the
// following line to:
//
// oActiveDoc.SaveAs(COleVariant("c:\\badpath\\doc1.doc"),
// COleVariant((short)0),
// vFalse, COleVariant(""), vTrue, COleVariant(""),
// vFalse, vFalse, vFalse, vFalse, vFalse);