本文跟大家分享的是VC操作Word的东东,使用的是Office2000,工程类型是MFC的对话框。
先看看需要使用到的东东: 1、View->ClassWizard->Automation->Add Class From Library:定位到你安装Office的目录找到MSWord9.Olb;2、打开后把所有的类都选中倒入到你的工程(反正也是搞一回彻底一点:P) ;3、在你的对话框头文件中加上#include"msword9.h"。OK了准备工作搞定之后就可以玩儿转Word了。
一、VC简单操作Word
先来一个简单的
C++代码
- void TestWord1()
- {
- _Application app;
- COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
- app.CreateDispatch(_T("Word.Application"));
- app.SetVisible(FALSE);
- //Create New Doc
- Documents docs=app.GetDocuments();
- CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
- docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
- //Add Content:Text
- Selection sel=app.GetSelection();
- sel.TypeText(_T("\t\t\t\t\t第一次玩儿Word\r\n"));
- sel.TypeText(_T("\t\t\t\t\t\t\t\t----------先来一个简单的\r\n"));
- sel.ReleaseDispatch();
- docs.ReleaseDispatch();
- app.SetVisible(TRUE);
- app.ReleaseDispatch();
- }
二、VC操作Word生成表格
大家都应该使用过Word提供的表格功能很是专业,如果在作一些数据库开发之类的软件对汇总的数据进行打印是常见的功能,这个如果用Word来实现个人感觉不错,废话少说说练咱就练。
C++代码
- void CWordTestDlg::TestWord2()
- {
- _Application app;
- COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
- app.CreateDispatch(_T("Word.Application"));
- app.SetVisible(FALSE);
- //Create New Doc
- Documents docs=app.GetDocuments();
- CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
- docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
- //Add Content:Text
- Selection sel=app.GetSelection();
- sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));
- sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));
- COleDateTime dt=COleDateTime::GetCurrentTime();
- CString strDT=dt.Format("%Y-%m-%d");
- CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
- str+=strDT;
- str+="\r\n";
- sel.TypeText(str);
- //Add Table
- _Document saveDoc=app.GetActiveDocument();
- Tables tables=saveDoc.GetTables();
- CComVariant defaultBehavior(1),AutoFitBehavior(1);
- tables.Add(sel.GetRange(),6,11,&defaultBehavior,&AutoFitBehavior);
- Table table=tables.Item(1);
- sel.TypeText(_T("Test1"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short
- (0)));
- sel.TypeText(_T("Test2"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test3"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test4"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short<(0)));
- sel.TypeText(_T("Test5"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test6"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test7"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test8"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test9"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test10"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test11"));
- app.SetVisible(TRUE);
- table.ReleaseDispatch();
- tables.ReleaseDispatch();
- sel.ReleaseDispatch();
- docs.ReleaseDispatch();
- saveDoc.ReleaseDispatch();
- app.ReleaseDispatch();
- }
上面说了如何制作一个简单的表格,在实际应用中会发现表格的种类很多其中对于单元格要求合并的情形很多,这样出来的表格比较专业让客户看起来也很是舒服,不喔喔了看看下面的代码就全明白了。
C++代码
- void CWordTestDlg::TestWord3()
- {
- _Application app;
- COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
- app.CreateDispatch(_T("Word.Application"));
- app.SetVisible(FALSE);
- //Create New Doc
- Documents docs=app.GetDocuments();
- CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
- docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
- //Add Content:Text
- Selection sel=app.GetSelection();
- sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));
- sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));
- COleDateTime dt=COleDateTime::GetCurrentTime();
- CString strDT=dt.Format("%Y-%m-%d");
- CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
- str+=strDT;
- str+="\r\n";
- sel.TypeText(str);
- //Add Table
- _Document saveDoc=app.GetActiveDocument();
- Tables tables=saveDoc.GetTables();
- CComVariant defaultBehavior(1),AutoFitBehavior(1);
- tables.Add(sel.GetRange(),7,11,&defaultBehavior,&AutoFitBehavior);
- Table table=tables.Item(1);
- sel.TypeText(_T("Test1"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test2"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test3"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test4"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test5"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test6"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test7"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test8"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test9"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test10"));
- sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
- sel.TypeText(_T("Test11"));
- for(int i=2;i<7;i+=2)
- {
- Cell c1=table.Cell(i,1);
- Cell c2=table.Cell(i+1,1);
- c1.Merge(c2);
- c1.ReleaseDispatch();
- c2.ReleaseDispatch();
- }
- app.SetVisible(TRUE);
- table.ReleaseDispatch();
- tables.ReleaseDispatch();
- sel.ReleaseDispatch();
- docs.ReleaseDispatch();
- saveDoc.ReleaseDispatch();
- app.ReleaseDispatch();
- app.SetVisible(TRUE);
- }
运行下看看效果,还不错吧?呵呵
除非特别注明,鸡啄米文章均为原创