怎么用vc实现查找word文档中的内容.待结果!立刻结分
如何用vc实现查找word文档中的内容.在线等待结果!立刻结分
例如,本人想查找某个word文档中是否包含我感兴趣的数字33445566.如果,有这个数字串我再进行一些其他处理.现在就怎么查找word中的内容提问,请教各位高手.
------解决方案--------------------
直接打开不行吗?我没试过 应该是可以的
------解决方案--------------------
下面是从****上摘抄过来的,主要是今天调试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.
例如,本人想查找某个word文档中是否包含我感兴趣的数字33445566.如果,有这个数字串我再进行一些其他处理.现在就怎么查找word中的内容提问,请教各位高手.
------解决方案--------------------
直接打开不行吗?我没试过 应该是可以的
------解决方案--------------------
下面是从****上摘抄过来的,主要是今天调试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.