转一篇以前看的资料,希望有帮助:VC操作Word2003例子下面是从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.aspQ179706 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.aspQ196776 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: #include "msword8.h" // msword9.h for Word 2000, msword.h for Word 20024. 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)... 希望对您有帮助!本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))
该文章只谈到如何读写里面的文字,我们又应该如何来识别它的字体呢,上下标呢,公式呢,表格呢以及一级二级等标题?
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.aspQ179706 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.aspQ196776 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: #include "msword8.h" // msword9.h for Word 2000, msword.h for Word 20024. 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)...
希望对您有帮助!本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))