Article ID: 178784
Article Last Modified on 1/22/2007
224925 INFO: Type Libraries for Office May Change with New Release
178749 How To Create an Automation Project Using MFC and a Type Library
#include "msword8.h" // msword9.h for Word 2000, msword.h for Word 2002
_Application objWord;
// Convenient values declared as ColeVariants.
COleVariant covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Get the IDispatch pointer and attach it to the objWord object.
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("C:\\Test.doc",VT_BSTR),
covFalse, // Confirm Conversion.
covFalse, // ReadOnly.
covFalse, // AddToRecentFiles.
covOptional, // PasswordDocument.
covOptional, // PasswordTemplate.
covFalse, // Revert.
covOptional, // WritePasswordDocument.
covOptional, // WritePasswordTemplate.
covOptional) // Format. // Last argument for Word 97
covOptional, // Encoding // New for Word 2000/2002
covTrue, // Visible
covOptional, // OpenConflictDocument
covOptional, // OpenAndRepair
(long)0, // DocumentDirection wdDocumentDirection LeftToRight
covOptional // NoEncodingDialog
) // Close Open parameters
); // Close AttachDispatch(…)
AfxMessageBox("Now printing 2 copies on the active printer");
testDoc.PrintOut(covFalse, // Background.
covOptional, // Append.
covOptional, // Range.
covOptional, // OutputFileName.
covOptional, // From.
covOptional, // To.
covOptional, // Item.
COleVariant((long)2), // Copies.
covOptional, // Pages.
covOptional, // PageType.
covOptional, // PrintToFile.
covOptional, // Collate.
covOptional, // ActivePrinterMacGX.
covOptional // ManualDuplexPrint.
covOptional, // PrintZoomColumn New with Word 2002
covOptional, // PrintZoomRow ditto
covOptional, // PrintZoomPaperWidth ditto
covOptional); // PrintZoomPaperHeight ditto
// If you wish to Print Preview the document rather than print it,
// you can use the PrintPreview member function instead of the
// PrintOut member function:
// testDoc[i].PrintPreview.
objWord.Quit(covFalse, // SaveChanges.
covTrue, // OriginalFormat.
covFalse // RouteDocument.
);
testDoc.AttachDispatch(docs.Open(
COleVariant("C:\\My Docs\\Test.doc",VT_BSTR)...
222101 How To Find and Use Office Object Model Documentation
Additional query words: IDispatch Word 8.0 Word8 Word97 x
Keywords: kbprint kbopenfile kbautomation kbprogramming kbhowto kbinterop KB178784