Q238972 INFO: Using Visual C++ to Automate Office
http://support.microsoft.com/support/kb/articles/q238/9/72.asp
Q196776 FAQ: Office Automation Using Visual C++
http://support.microsoft.com/support/kb/articles/q196/7/76.asp
此外,我在以下列出了Q178784中的样例代码,并添加了一些中文注释。
Steps to Create the Project
---------------------------
1. 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.
2. 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
请先按照Q178749的步骤建立一个框架程序,并引入Word typelib.
3. At the top of the AutoProjectDlg.cpp, add the following line:
#i nclude "msword8.h" // msword9.h for Word 2000, msword.h for Word 2002
4. Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDLG.cpp
file.
// 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.
// 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.
5. 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: