In Microsoft Word, create a new document, add some text to the document, and save it as Test.doc. Close the document and exit Word. Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the MSWord8.olb type library: Q178749 HOWTO: Create an Automation Project Using MFC and a Type Library At the top of the AutoProjectDlg.cpp, add the following line: #include "msword8.h" // msword9.h for Word 2000, msword.h for Word 2002 Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDLG.cpp file. Sample Code _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. ); You may need to modify the code in CAutoProjectDlg::OnRun() to indicate the correct path for your document Test.doc. The document is referenced in the following line: testDoc.AttachDispatch(docs.Open( COleVariant("C:\\My Docs\\Test.doc",VT_BSTR)...
由于IE和Word 都是COM组件技术实现的,你可以使用COM找到文档接口,其他得好办了,关于COM的书多数有讲Word一类的例子,IE的例子课到MSDN中去找,用BHO做关键字查找
Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the MSWord8.olb type library:
Q178749 HOWTO: Create an Automation Project Using MFC and a Type Library
At the top of the AutoProjectDlg.cpp, add the following line:
#include "msword8.h" // msword9.h for Word 2000, msword.h for Word 2002
Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDLG.cpp file.
Sample Code
_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.
);
You may need to modify the code in CAutoProjectDlg::OnRun() to indicate the correct path for your document Test.doc. The document is referenced in the following line:
testDoc.AttachDispatch(docs.Open(
COleVariant("C:\\My Docs\\Test.doc",VT_BSTR)...