完整程序及word文件从下载:http://www.pudn.com/downloads407/sourcecode/windows/file/detail1734028.html
把111.doc拷贝到c盘目录下
1.创建一个基于对话框的程序,选择默认设置即可;
2. 添加新类:【查看】-> 【建立类向导】->【Add Class】-> 【From a type Library】,选择:C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB文件(视office安装目录),在出现的类添加中,选择_Application, _Document, Documents,Selection四个类;环境会创建了文件msword.h,msword.cpp两个文件;
3. 在CTest1App::InitInstance()添加
if (!AfxOleInit())
{
return FALSE;
}
4. 在对话框中添加一个按钮,双击,里面程序如下:
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("创建Word2003服务失败!");
exit(1);
}
//下面是定义VARIANT变量;
COleVariant varFilePath("c:\\111.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,
vOpt, vOpt, vOpt, vOpt);
//打开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,
vOpt, vOpt, vOpt, vOpt, vOpt);
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);
上面中的 MoveDown程序是从word录制宏得来,
Sub Macro1()
'
' Macro1 Macro
' 宏在 2011-12-17 由 微软中国 录制
'
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="12345"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="丽丽"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="122"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="艺术"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="无"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="上海市虹口区民主路315号"
ActiveDocument.Save
End Sub
其中常量值用调试器可以看到:
wdLine = 5;
wdCharacter = 1;
wdCell = 12;