以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  一个word模板有五页 有没有办法根据条件删除其中的某个页  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=174941)

--  作者:cnsjroom
--  发布时间:2022/2/12 14:16:00
--  一个word模板有五页 有没有办法根据条件删除其中的某个页

一个word模板有五页  有没有办法根据条件删除其中的某个页

 

有时候需要显示五页

有时候需要删除第5页  或第3页  或者3.4.5页都删除

 

怎么操作才能实现呢?


--  作者:有点蓝
--  发布时间:2022/2/12 14:47:00
--  
参考:https://blog.csdn.net/zp2zjl/article/details/7988753
https://docs.microsoft.com/zh-cn/office/vba/api/word.range.delete

大概这样
Dim pages As Integer
Dim fl As String = ProjectPath & "Attachments\\" & "123.docx"
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open(fl)
    
    Dim range1 = doc.GoTo(1, Which:=1, Count:=5) 第几页就把Count:=5改为几,比如第三页:Count:=3
    Dim range2 = range1.GoToNext(1)
    doc.Range(range1.Start,range2.Start).Delete(1,1)
    doc.save
    Doc.close
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try

--  作者:cnsjroom
--  发布时间:2022/2/12 15:51:00
--  回复:(有点蓝)参考:https://blog.csdn.net/zp2zjl...

老师  尝试了一下  没有成功   还得继续麻烦老师  谢谢!

 

当前代码如下:【第四页并没有被删除,刚好第四页是末尾页,另外如果我要删除3.4页,下述红色部分代码我该怎么写呢】

Dim tm As String  = ProjectPath & "Attachments\\谈话通知书.doc" \'指定模板文件
Dim fl As String = ProjectPath & "Reports\\谈话通知书.doc" \'指定目标文件
Dim fl2 As String = ProjectPath & "Reports\\谈话通知书.pdf" \'指定目标文件


Dim wrt As New WordReport(Tables("谈话通知书"),tm,fl) \'定义一个WordReport
wrt.Build() \'逐行生成报表
wrt.SaveToPDF(fl2) \'保存为PDF文件
wrt.Quit() \'退出

Dim pages As Integer
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open(fl)    
    Dim range1 = doc.GoTo(1, Which:=1, Count:=4) \'第几页就把Count:=5改为几,比如第三页:Count:=3
    Dim range2 = range1.GoToNext(1)
    doc.Range(range1.Start,range2.Start).Delete(1,1)
    doc.save
    Doc.close
catch ex As exception
    msgbox(ex.message)
finally
    app.Quit
End try

Dim Proc As New Process \'打开PDF文件
Proc.File = fl
Proc.Start()

[此贴子已经被作者于2022/2/12 15:55:01编辑过]

--  作者:有点蓝
--  发布时间:2022/2/12 16:18:00
--  
没有删除页的用法,实现的是删除这一页的所有内容,后面的页就顶上了。因为是最后一页,没有其它页顶上,就会存在一个空白页

删除3,4页,分别删除即可:删除2次第三页;或者先删除第4页,再删除第3页

--  作者:程兴刚
--  发布时间:2022/2/12 17:59:00
--  
也就这5页,把他拆分装入数据表,一键生成文档,想要哪一行装入哪一行,非常灵活高效!