注册 登录
编程论坛 VFP论坛

如何用VFP代码控制WORD每页打印25条记录

王咸美 发布于 2021-05-24 07:57, 3435 次点击
WORD模板格式如下:
只有本站会员才能查看附件,请 登录

我想用VFP代码控制WORD每页打印25条记录,
wordapp.ActiveDocument.PageSetup.LinesPage=25这句好象不起作用,
不知代码如何写,请高手赐教,万分感谢!!!
13 回复
#2
xuminxz2021-05-24 08:31
程序代码:

wdrs=Createobject('word.application')
wjm=Sys(5)+Sys(2003)+"\测试"
wdrs.Visible=.T.  
wdrs.documents.Open(wjm)
wdrs.activedocument.Tables(1).cell(26,1).select &&选定28行第一个单元格
wdrs.Selection.InsertBreak(7)&&在28行前、27行后(25条记录)插入分页


[此贴子已经被作者于2021-5-24 08:32编辑过]

#3
王咸美2021-05-24 09:37
@xuminxz 感谢热心指点!表中有很多记录,我想每25条记录分为一页,不知下列代码错在哪里?望赐教!
yms=INT(RECCOUNT()/25)
FOR i=1 TO yms
  WordApp.ActiveDocument.Tables(1).Cell((i*25+3),1).Select
  WordApp.Selection.InsertBreak(7) &&插入分页
ENDFOR
#4
xuminxz2021-05-24 20:00
没问题吧?什么错误?
#5
schtg2021-05-25 07:04
学习啦,谢谢分享!
#6
王咸美2021-05-25 07:40
@xuminxz 我把程序及问题截图发上来,请帮我看看代码有什么问题?
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录

程序代码如下:
* VFP调用WORD模板文件生成多页WORD文档
* 设置"标题行重复":打开新生成的WORD文档,选中标题行->单击表格->单击标题行重复

CLEAR ALL
CLOSE ALL
SET TALK OFF
SET SAFETY OFF
SET DATE TO YMD
SET MARK TO "-"
SET CENTURY ON
SET COMPATIBLE OFF

cCurrentProcedure = SYS(16,1)
nPathStart = AT(":",cCurrentProcedure)- 1
nLenOfPath = RAT("\", cCurrentProcedure) - (nPathStart)
mypath=SUBSTR(cCurrentProcedure, nPathStart, nLenofPath)
SET Default TO (mypath)

wjm="F:\temp2\股票信息统计表模板.doc"
WordApp=CREATEOBJECT("Word.application")
WordApp.Visible =.t.
WordTable=WordApp.Application.Documents.Open[wjm] && 关键
use gp.dbf
WordApp.ActiveDocument.Tables(1).Cell(3,1).Select
WordApp.Selection.InsertRowsBelow(RECCOUNT()-1) && 插入行


FOR i=1 TO FCOUNT()
FOR k=3 TO RECCOUNT()+2  && word模板表头有2行
GO k-2
WordCellText=EVALUATE(FIELD(i))
DO CASE
CASE ISNULL(WordCellText)
WordCellText=""
CASE TYPE("WordCellText")="N"
CASE TYPE("WordCellText")="D"
WordCellText=DTOC(WordCellText)
CASE TYPE("WordCellText")="T"
WordCellText=TTOC(WordCellText)
CASE TYPE("WordCellText")="M"
WordCellText=TRIM(WordCellText)
WordCellText=STRTRAN(WordCellText,CHR(13),' ')
CASE TYPE("WordCellText")="L"
WordCellText=IIF(WordCellText,".T.",".F.")
CASE EMPTY(WordCellText)
WordCellText=" "
OTHERWISE
* WordCellText=""
ENDCASE
IF EMPTY(WordCellText) OR ISNULL(WordCellText)
WordCellText=" "
ENDIF
WordTable.Tables.Item(1).Cell(k,i).Range.Text=WordCellText
ENDFOR
ENDFOR

yms=INT(RECCOUNT()/20)
FOR i=1 TO yms
  WordApp.ActiveDocument.Tables(1).Cell(i*20+4,1).Select
  WordApp.Selection.InsertBreak(7)
ENDFOR


WordApp.ActiveDocument.PageSetup.FooterDistance=19.0*2.835   &&页脚位置
*WordApp.ActiveDocument.PageSetup.Footer.fontsize=12
*mydate=subst(dtos(date()),1,4)+"年"+subst(dtos(date()),5,2)+;
*"月"+subst(dtos(date()),7,2)+"日"
*WordApp.Documents(1).Sections(1).Headers(1).Range.Text="制表日期: ;
*"+mydate+" "         &&页
WordApp.Documents(1).Sections(1).Headers(1).Range.Paragraphs.Alignment=2            && 页眉右齐
WordApp.Documents(1).Sections(1).Footers(1).Range.Paragraphs.Alignment=1            && 页脚居中
WordApp.Documents(1).Sections(1).Footers(1).Range.Select
WordApp.Selection.Font.Size=12
WordApp.Selection.InsertAfter("第")
WordApp.Selection.Start =WordApp.Selection.End
WordApp.Selection.InsertFormula("PAGE")
WordApp.Selection.Start =WordApp.Selection.End
WordApp.Selection.InsertBefore("页/共")
WordApp.Selection.Start =WordApp.Selection.End
WordApp.Selection.InsertFormula("NUMPAGES")
WordApp.Selection.Start =WordApp.Selection.End
WordApp.Selection.InsertBefore("页")
 
WordApp.Documents(1).SaveAs("F:\temp2\股票信息统计表.doc") &&自动保存文件
RELEASE WordApp
WAIT CLEAR
MessageBox( "生成Word文件完毕,文件位置 F:\temp2\股票信息统计表.doc!",64,"完毕")
RETURN

#7
xuminxz2021-05-25 13:50
插入分页符后,就拆分为两表了。是否每个表格都要保留表头?
如不需要,可按下列进行
WordApp.ActiveDocument.Tables(1).Cell(23,1).Select
WordApp.Selection.InsertBreak(7)
For i=2 To yms
    WordApp.ActiveDocument.Tables(i).Cell(21,1).Select
    WordApp.Selection.InsertBreak(7)
Endfor
如果需要,先打开模板表,将其复制到粘贴板。
每20个记录后粘贴一个模板到新表。
#8
xuminxz2021-05-25 14:00
wdrs.documents.Open(_sywj)  &&打开模板
Wors.Documents.SaveAs(otwjm)&&另存文件
wdrs.Selection.WholeStory() &&全选
wdrs.Selection.Copy()  &&复制到粘贴板
wdrs.Selection.EndKey(6) &&将光标移动到文档尾
wdrs.Selection.InsertBreak(7) &&插入分页。
wdrs.Selection.pasteandformat(19)  &&粘贴模板
#9
王咸美2021-05-25 17:02
@xuminxz 谢谢!!!模板文件连标题共4行(见1楼),我想生成的EXCEL文档每页都有标题行,且每页20条记录,如何操作呢?盼指点。
#10
xuminxz2021-05-25 17:29
在你的另一个帖子里回答了啊。
#11
王咸美2021-05-25 17:45
@xuminxz 说错了,不好意思。WORD模板文件连标题共4行(见1楼),我想生成的WORD文档每页都有标题行,且每页20条记录,如何操作呢?盼指点。
#12
xuminxz2021-05-25 21:03
这是每页20行数据,25行可以自己改


程序代码:

* 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编辑过]

#13
王咸美2021-05-26 07:33
@xuminxz 非常感谢耐心指导!!!美中不足的是最后插入了一页空白页。
#14
xuminxz2021-05-26 08:07
* VFP调用WORD模板文件生成多页WORD文档
* 设置"标题行重复":打开新生成的WORD文档,选中标题行->单击表格->单击标题行重复

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)
wdrs.Selection.WholeStory()
wdrs.Selection.Cut()  &&复制到粘贴板备用
Go Top
For i=0 To yms-1
    j=3
    wdrs.Selection.InsertBreak(7)   &&插入分页符 提前了2行,这样空白页在第一页
    wdrs.Selection.EndKey(6) &&将光标移动到文档尾
    wdrs.Selection.pasteandformat(19)  &&粘贴模板
    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.ActiveDocument.Range(0,1).Delete&&删除第一个空白页
wdrs.Documents(1).Save &&自动保存文件
Release wdrs
Wait Clear
Messagebox( "生成Word文件完毕,文件位置 "+_onm,64,"完毕")
Return


[此贴子已经被作者于2021-5-26 08:10编辑过]

1