下面是从CSDN上摘抄过来的,主要是今天调试VC操作Word2003的时候,网上下载的是操作Word 2000,但是参数已经有一些不同了。下面的代码可以在2003中运行,记录在这里便于以后用。

感谢您使用微软产品。

对于您所提的问题,确实可以使用OLE Automation在VC++中对Word Object Model进行操作。下面这篇知识库文章中给出了如何在VC_++中引入Office TypeLib,并通过程序启动MS Excel.参照这篇文章可以使您建立起程序的框架

Q178749 HOWTO: Create Automation Project Using MFC and a Type Library
http://support.microsoft.com/support/kb/articles/q178/7/49.asp

以下两篇知识库文章给出了具体的样例,如何操作Word和Excel. 您可以使用其中的方法来完成你自己的操作。具体的对象模型的操作,您需要参见对应产品的VBA帮助文档。

Q178784 HOWTO: Use Automation to Open and Print a Word Document
http://support.microsoft.com/support/kb/articles/q178/7/84.asp

Q179706 HOWTO: Use MFC to Automate Excel and Create/Format a New Workboo
http://support.microsoft.com/support/kb/articles/q179/7/06.asp

这两篇是介绍一些基础的知识以及Office 产品在Automation 上的一些支持以及常见问题。您可以用作参考。

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.

当以上步骤完成后,你会看到项目中有很多新的类,那些类就对应着Word的对象模型。

Sample Code
-----------

          _Application objWord; //定义Word应用程序对象(Word.application)

         // 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());//定义Word Documents对象(Word.Documents)

         _Document testDoc; //定义Word Document对象(Word.Document)

         testDoc.AttachDispatch(docs.Open( //可看成VB语句set testDoc =  Word.documents.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. //可看成VB语句testDoc.PrintOut(…)
                           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.
                      );

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:

         testDoc.AttachDispatch(docs.Open(
                               COleVariant("C:\\My Docs\\Test.doc",VT_BSTR)...


希望对您有帮助!