@xuminxz 说错了,不好意思。WORD模板文件连标题共4行(见1楼),我想生成的WORD文档每页都有标题行,且每页20条记录,如何操作呢?盼指点。
程序代码:
* VFP调用WORD模板文件生成多页WORD文档
* 设置"标题行重复":打开新生成的WORD文档,选中标题行[color=#808080]->单击表格->单击标题行重复[/color]
Close Tables All
Set Talk Off
Set Safety Off
Set Date To YMD
Set Mark To "-"
Set Century On
Set Compatible Off
Use gp
cCurrentProcedure = Sys(16,1)
nPathStart = At(":",cCurrentProcedure)- 1
nLenOfPath = Rat("\", cCurrentProcedure) - (nPathStart)
mypath=Substr(cCurrentProcedure, nPathStart, nLenofPath)
Set Default To (mypath)
_fnm=Sys(5)+Sys(2003)+"\股票信息统计表模板.doc"
_Onm=Sys(5)+Sys(2003)+"\股票信息统计表.doc"
Declare Long SetForegroundWindow In user32.Dll Long &&设置顶层窗口
Declare Long FindWindow In WIN32API String lpClassName,String lpWindowName &&第一个参数写 null才行!
oWrd_hWnd=FindWindow(Null,Justfname(_fnm)+' - Word')
If oWrd_hWnd<>0
SetForegroundWindow(owrd_hwnd)
wdrs=Getobject(,'word.application')
wdrs.WindowState=2 && 0 普通 1 最大化 2 最小化
Else
wdrs=Createobject('word.application') &&创建Word目标测试是否安装了word *
wdrs.documents.Open(_fnm)
Endif
wdrs.Visible=.T.
wdrs.activedocument.SaveAs(_Onm)
**增加空行,使总数等于20
**代码自己写吧但建议直接在模板文件中设置好
**
yms=CEILING(Reccount()/20) &&不能用int ,会丢掉数据。
wdrs.Selection.WholeStory()
wdrs.Selection.Cut() &&复制到粘贴板备用
Go Top
For i=0 To yms-1
j=3
wdrs.Selection.EndKey(6) &&将光标移动到文档尾
wdrs.Selection.pasteandformat(19) &&粘贴模板
wdrs.Selection.InsertBreak(7) &&插入分页符
Do While !Eof() And j<23
wdrs.activedocument.Tables(i+1).Cell(j,1).Range.Text=Evaluate(Field(1))
wdrs.activedocument.Tables(i+1).Cell(j,2).Range.Text=Evaluate(Field(2))
wdrs.activedocument.Tables(i+1).Cell(j,3).Range.Text=Evaluate(Field(3))
wdrs.activedocument.Tables(i+1).Cell(j,4).Range.Text=Evaluate(Field(4))
wdrs.activedocument.Tables(i+1).Cell(j,5).Range.Text=Evaluate(Field(5))
wdrs.activedocument.Tables(i+1).Cell(j,6).Range.Text=Evaluate(Field(6))
wdrs.activedocument.Tables(i+1).Cell(j,7).Range.Text=Evaluate(Field(7))
wdrs.activedocument.Tables(i+1).Cell(j,8).Range.Text=Evaluate(Field(8))
wdrs.activedocument.Tables(i+1).Cell(j,9).Range.Text=Evaluate(Field(9))
wdrs.activedocument.Tables(i+1).Cell(j,10).Range.Text=Evaluate(Field(10))
wdrs.activedocument.Tables(i+1).Cell(j,11).Range.Text=Evaluate(Field(11))
wdrs.activedocument.Tables(i+1).Cell(j,12).Range.Text=Evaluate(Field(12))
wdrs.activedocument.Tables(i+1).Cell(j,13).Range.Text=Evaluate(Field(13))
wdrs.activedocument.Tables(i+1).Cell(j,14).Range.Text=Evaluate(Field(14))
wdrs.activedocument.Tables(i+1).Cell(j,15).Range.Text=Evaluate(Field(15))
wdrs.activedocument.Tables(i+1).Cell(j,16).Range.Text=Evaluate(Field(16))
j=j+1
Skip
Endd
Endfor
wdrs.ActiveDocument.PageSetup.FooterDistance=19.0*2.835 &&页脚位置
*wdrs.ActiveDocument.PageSetup.Footer.fontsize=12
*mydate=subst(dtos(date()),1,4)+"年"+subst(dtos(date()),5,2)+;
*"月"+subst(dtos(date()),7,2)+"日"
*wdrs.Documents(1).Sections(1).Headers(1).Range.Text="制表日期: ;
*"+mydate+" " &&页
wdrs.Documents(1).Sections(1).Headers(1).Range.Paragraphs.Alignment=2 && 页眉右齐
wdrs.Documents(1).Sections(1).Footers(1).Range.Paragraphs.Alignment=1 && 页脚居中
wdrs.Documents(1).Sections(1).Footers(1).Range.Select
wdrs.Selection.Font.Size=12
wdrs.Selection.InsertAfter("第")
wdrs.Selection.Start =wdrs.Selection.End
wdrs.Selection.InsertFormula("PAGE")
wdrs.Selection.Start =wdrs.Selection.End
wdrs.Selection.InsertBefore("页/共")
wdrs.Selection.Start =wdrs.Selection.End
wdrs.Selection.InsertFormula("NUMPAGES")
wdrs.Selection.Start =wdrs.Selection.End
wdrs.Selection.InsertBefore("页")
wdrs.Documents(1).Save &&自动保存文件
Release wdrs
Wait Clear
Messagebox( "生成Word文件完毕,文件位置 "+onm,64,"完毕")
Return
[此贴子已经被作者于2021-5-25 21:08编辑过]




