vc++使用word

来源:互联网 发布:c语言标准函数库 编辑:程序博客网 时间:2024/04/29 05:36

第一步,导入word对象库 #import "d:/Program Files/Microsoft Office/OFFICE11/MSWORD.OLB" raw_interfaces_only raw_native_types named_guids / rename("ExitWindows","ExitWindows1")

using namespace Word;

需要将ExitWindows重新命名,因为与系统冲突;使用名字空间是为了防止与其他组件冲突

下面是一段往表格里填写文字的代码:

using namespace Word;

 _ApplicationPtr app; app.CreateInstance(__uuidof(Word::Application));  

_DocumentPtr wdDoc; 

DocumentsPtr wdDocs; 

app->get_Documents(&wdDocs); 

CComVariant vOpt(DISP_E_PARAMNOTFOUND,VT_ERROR); 

CComVariant vr(filePah); 

wdDocs->Open(&vr,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&wdDoc); 

TablesPtr tables; 

wdDoc->get_Tables(&tables); 

TablePtr table; tables->Item(1,&table); //第一个表格,不是从0开始的,所有的项都是从1开始的,不是程序的惯例从0开始

CellPtr cell;

 RangePtr range;

 CComBSTR bstr; 

bstr=L“xxxx”; 

table->Cell(2,2,&cell);//第二行第二列

 cell->get_Range(&range); 

range->put_Text(bstr); 

 RowPtr row;  

rows->Item(3,&row);  

row->Delete();  //删除一行 

 CComVariant vrSave(sFile); 

 wdDoc->SaveAs(&vrSave);   //保存文件

   wdDoc->Close(); 

app->Quit();

原创粉丝点击