分类: C/C++
2008-03-31 14:49:12
|
// _Application wrapper class class _Application : public COleDispatchDriver { ...... }至于这些类和成员函数的功能和用法,我有一个简便的方法,那就是使用word中工具菜单中的宏的录制功能,把你所想要进行的操作先用宏录制下来,然后查看这些宏代码,你就会清楚要使用哪个类,哪些成员函数和成员函数应该带些什么参数了。虽然这些代码都是用VB写的,但你可以很容易的转换成VC++中的代码。
if (!AfxOleInit()) { AfxMessageBox(IDP_OLE_INIT_FAILED); return FALSE; }3.接下来选择对话框资源IDD_WORDAUTOMATION_DIALOG,在对话框中增加一个按钮命名为IDC_WORD_NEW,在按钮的处理函数中增加如下的代码:
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //开始一个新的Microsoft Word 2000实例 _Application oWordApp; if (!oWordApp.CreateDispatch("Word.Application", NULL)) { AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND); return; } //创建一个新的word文档 Documents oDocs; _Document oDoc; oDocs = oWordApp.GetDocuments(); oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //如果是word 98,则应该带两个参数,如oDocs.Add(vOpt, vOpt) //把文本添加到word文档 Selection oSel; oSel = oWordApp.GetSelection(); oSel.TypeText("one"); oSel.TypeParagraph(); oSel.TypeText("two"); oSel.TypeParagraph(); oSel.TypeText("three"); //保存word文档 _Document oActiveDoc; oActiveDoc = oWordApp.GetActiveDocument(); oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"), COleVariant((short)0), vFalse, COleVariant(""), vTrue, COleVariant(""), vFalse, vFalse, vFalse, vFalse, vFalse); //退出word应用程序 oWordApp.Quit(vOpt, vOpt, vOpt);4.在WoreAutomation.cpp中增加包含头文件msword9.h。
#include "msword9.h" //或者在word98中是 "msword8.h"注意:增加的头文件应该在stdafx.h文件之后,否则就会导致编译错误。
BeginWaitCursor(); COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); _Application m_App;//定义Word提供的应用程序对象; Documents m_Docs;//定义Word提供的文档对象; Selection m_Sel;//定义Word提供的选择对象; m_Docs.ReleaseDispatch(); m_Sel.ReleaseDispatch(); m_App.m_bAutoRelease=true; if(!m_App.CreateDispatch("Word.Application")) { AfxMessageBox("创建Word2000服务失败!"); exit(1); } //下面是定义VARIANT变量; COleVariant varFilePath("word表格.doc"); COleVariant varstrNull(""); COleVariant varZero((short)0); COleVariant varTrue(short(1),VT_BOOL); COleVariant varFalse(short(0),VT_BOOL); m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来; m_Docs.Open(varFilePath,varFalse,varFalse,varFalse, varstrNull,varstrNull,varFalse,varstrNull, varstrNull,varTrue,varTrue,varTrue); //打开Word文档; m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来; m_Sel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0)); m_Sel.MoveDown(COleVariant((short)5),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("123456789"); m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("李明"); m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("25"); m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("技术员"); m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("本科"); m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0)); m_Sel.TypeText("上海市虹口区民主路315号"); //save word file _Document oActiveDoc; oActiveDoc = m_App.GetActiveDocument(); oActiveDoc.SaveAs(COleVariant("c:\\填写后表格.doc"), COleVariant((short)0), vFalse, COleVariant(""), vTrue, COleVariant(""), vFalse, vFalse, vFalse, vFalse, vFalse); m_Docs.ReleaseDispatch();//断开关联; m_Sel.ReleaseDispatch(); //退出WORD m_App.Quit(vOpt, vOpt, vOpt); m_App.Quit(vOpt, vOpt, vOpt); m_App.ReleaseDispatch(); EndWaitCursor(); MessageBox("word表格填写完毕!","提示",MB_ICONEXCLAMATION);注意:要运行本文提供的源程序,需要预先安装Microsoft Word 2000。