1. 给工程“添加类”,然后选择“TypeLib 中的 MFC 类”
2. 选择 Microsoft Word 12.0 object Library 这个库的版本根据你的 office 版本确定
3. 从“接口”一列中依次选择下面几个到“生成的类”中
_Application, _Document, Document, Selection
4. 工程文件中会新添加上面选中的对象对应的四个文件
5. 还需要在 App 的 InitInstance 中注册 com 组键 CoInitialize(NULL);
ExitInstance 中 CoUninitialize();
6. 使用代码如下:
//加入到项目的 word 控件的源文件其实就是 office 的 msword.olb 这个文件,所以下面这个
路径根据你的实际情况调整
import "C:\Program Files\Microsoft Office\Office12\MSWORD.OLB" named_guids
raw_interfaces_only rename("ExitWindows", "wordExitWindows")
#include "msword9.h"
#include "CApplication.h"
#include "CDocument0.h"
#include "CDocuments.h"
#include "CSelection.h"
#include "CTables0.h"
#include "CTable0.h"
#include "CParagraphFormat.h"
#include "CFont0.h"
#include "CRange.h"
#include "CCell.h"
#include "CBorders.h"
Void Test(void)
{
COleVariant vtrue((short)true),
vfalse((short)false),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //开始一个
新的Microsoft Word 2000实例
CApplication oWordApp;
if (!oWordApp.CreateDispatch(_T("Word.Application"), NULL))
{
AfxMessageBox(_T("创建Word2000文档失败!"), MB_OK | MB_SETFOREGROUND);
return;
}
oWordApp.put_Visible(FALSE);
//创建一个新的word文档
CDocuments oDocs; CDocument0 oDoc;
oDocs = oWordApp.get_Documents();
oDocs.Add(vOpt, vOpt, vOpt, vOpt);
oDoc = oWordApp.get_ActiveDocument();
//如果是word 98,则应该带两个参数,如oDocs.Add(vOpt, vOpt)
//把文本添加到word文档
CSelection oSel;
oSel = oWordApp.get_Selection();
CFont0 oFont;
CParagraphFormat wordFormat;
//标题
VARIANT varUnit;
VARIANT varOptional;
VariantInit(&varUnit);
varUnit.vt=VT_I4;
varUnit.lVal=5;
VariantInit(&varOptional);
varOptional.vt=VT_ERROR;
varOptional.scode=DISP_E_PARAMNOTFOUND;
oSel.HomeKey(&varUnit,&varOptional);
oFont=oSel.get_Font();
oFont.put_Size(20);
oFont.put_Bold(1);
oFont.put_Name(L"宋体");
wordFormat=oSel.get_ParagraphFormat();
wordFormat.put_Alignment(1);
oSel.put_ParagraphFormat(wordFormat);
oSel.TypeText(_T("测试报表\r\n"));
oSel.EndKey(&varUnit,&varOptional);
//表格
VARIANT varTable;
VariantInit(&varTable);
varUnit.vt=VT_I4;
varUnit.lVal=0;
int nRows = 2;
int nCols = 4;
CRange wordRange= oSel.get_Range();
CTables0 tables=oSel.get_Tables();
CTable0 tab0=tables.Add(wordRange,nRows,nCols,&varOptional,&varTable);
CString strField[4];
strField[0]=L"测试人员";
strField[1]=L"测试时间";
strField[2]=L"测试编号";
strField[3]=L"备 注";
CCell tblCell;
CBorders tblBorders;
tblBorders=tab0.get_Borders();
tblBorders.put_InsideLineStyle(1);
tblBorders.put_OutsideLineStyle(1);
for(int nRow=1;nRow<=nRows;nRow++)
{
for(int nCol=1;nCol<=nCols;nCol++)
{
tblCell=tab0.Cell(nRow,nCol);
wordRange=tblCell.get_Range();
oFont=wordRange.get_Font();
oFont.put_Name(L"宋体");
oFont.put_Size(10);
oFont.put_Bold(0);
wordRange.put_Font(oFont);
wordFormat=wordRange.get_ParagraphFormat();
wordFormat.put_Alignment(1);
wordRange.put_ParagraphFormat(wordFormat);
if(nRow == 1)
wordRange.InsertAfter(strField[nCol-1]);
else
{
switch(nCol)
{
case 1:
wordRange.InsertAfter(L "某某");
break;
case 2:
{
CString csTime(L"2012-05-30 11:11:11");
wordRange.InsertAfter(csTime);
}
break;
case 3:
wordRange.InsertAfter(L"001-a");
break;
case 4:
wordRange.InsertAfter(L"测试");
break;
}
}
}
}
oWordApp.put_Visible(TRUE);
oWordApp.put_WindowState(1);
oDoc.Activate();
tables.ReleaseDispatch();
tblCell.ReleaseDispatch();
tab0.ReleaseDispatch();
tblBorders.ReleaseDispatch();
oFont.ReleaseDispatch();
wordFormat.ReleaseDispatch();
wordRange.ReleaseDispatch();
oDocs.ReleaseDispatch();
oDoc.ReleaseDispatch();
oSel.ReleaseDispatch();
oWordApp.ReleaseDispatch();
}