关于用vc++生成word自动化文档的一些问题
我需要用vc++将程序运行的结果保存到word的表格中,另外,我还需加入页眉(包括汉字和一个小图标)和页脚(包括文档的总页数和当前页码)请问该如何实现这个功能?如有知道者请速告之,邮箱:tinai@163.com 谢谢! 问题点数:0、回复次数:1Top
1 楼Onega(www.fruitfruit.com)回复于 2004-05-03 21:58:29 得分 0
http://support.microsoft.com/?scid=kb;EN-US;Q293861
HOWTO: Use Word Automation to Count the Number of Pages in Each Section of a Document
Follow steps 1 through 12 in the following article in the Microsoft Knowledge Base to create a sample project that uses the IDispatch interfaces and member functions that are defined in the MSWord9.olb type library:
178749 HOWTO: Create an Automation Project Using MFC and a Type Library
At the top of AutoProjectDlg.cpp, add the following line:#include "MSWord9.h"
Add the following code to CAutoProjectDlg::OnRun() in AutoProjectDlg.cpp:COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//Start Word.
_Application oWord;
oWord.CreateDispatch("Word.Application");
oWord.SetScreenUpdating(FALSE);
//Open the document.
Documents oDocs = oWord.GetDocuments();
_Document oDoc = oDocs.Open(COleVariant("c:\\mydoc.doc"),
vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt, vOpt, vOpt);
//Repaginate the document.
oDoc.Repaginate();
//Iterate the collection of sections in the document to retrieve the page
//count for each section.
Sections oSecs = oDoc.GetSections();
long NumSections = oSecs.GetCount();
long i;
long StartPage=1; //section start page.
long EndPage=0; //section end page.
long NumPages=0; //number of pages in the section.
for(i=1;i<=NumSections;i++)
{
Section oSec = oSecs.Item(i);
Range oSecRange = oSec.GetRange();
VARIANT vInfo = oSecRange.GetInformation(3L);//wdActiveEndPageNumber=3
//If oSec.Index = NumSections Then nEndPg = nEndPg + 1
if(oSec.GetIndex()== NumSections) {EndPage++;}
EndPage = vInfo.lVal-1;
if(i==NumSections) {EndPage++;} //Account for the last section.
NumPages = EndPage - StartPage +1;
char buf[5];
sprintf(buf,"%d", NumPages);
TRACE1("Section %d\n", oSec.GetIndex());
TRACE3(" StartPage: %d EndPage: %d TotalPages: %d\n",
StartPage, EndPage, NumPages);
StartPage = EndPage + 1;
}
//Close the document without saving changes and quit Word.
oDoc.Close(COleVariant((short)false), vOpt, vOpt);
oWord.Quit(COleVariant((short)false), vOpt, vOpt);
Compile and run the project.
When the dialog box appears, click Run. The count results are displayed in the Debug window.
Top




