vba - 将文档从 2 到 7 页拆分为每页一个新文档
问题描述
我正在尝试用 word 编写代码(宏),我可以在其中将范围定义为页面并将页面从 2 复制到新文档中的 7?
Sub SaveSelected()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=7
Documents.Add , wdNewBlankDocument
Range.Paste
ActiveDocument.Save
End Sub
目前我遇到的问题是代码不起作用。
- 将页面定义为范围
- 复制范围
- 打开一个新的 (.docx)
- 另存为窗口(弹出)
解决方案
例如:
将范围内的所有页面保存到新文档
Sub SaveSelected()
Dim newDoc As Document
Dim src As Range
Dim pages As Range
'set src range to start at page 2
Set src = ActiveDocument.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2)
'extend range to the end of page 7
Set pages = ActiveDocument.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute, Count:=7)
src.End = pages.GoTo(what:=wdGoToBookmark, Name:="\page").End
Set newDoc = Documents.Add(, wdNewBlankDocument)
newDoc.Content.FormattedText = src.FormattedText
'remove extra paragraph at end
With newDoc.Paragraphs.Last.Range
If Len(.Text) = 1 Then .Delete
End With
newDoc.Paragraphs.Last.Range.Delete
newDoc.Save
End Sub
将范围内的每一页保存到新文档
Sub SavePages()
SavePageToNewDocument ActiveDocument, 2, 7
End Sub
Sub SavePageToNewDocument(SourceDoc As Document, FirstPage As Long, LastPage As Long)
Dim pageNum As Long
Dim srcPage As Range
Dim footer As Range
Dim newDoc As Document
For pageNum = FirstPage To LastPage
Set srcPage = SourceDoc.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageNum)
srcPage.End = srcPage.GoTo(what:=wdGoToBookmark, Name:="\page").End
Set footer = srcPage.Sections(1).Footers(wdHeaderFooterPrimary).Range
Set newDoc = Documents.Add(, wdNewBlankDocument)
'take across text and formatting
newDoc.Range.FormattedText = srcPage.FormattedText
With newDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
.FormattedText = footer.FormattedText
'remove extra paragraph at end
With .Paragraphs.Last.Range
If Len(.Text) = 1 Then .Delete
End With
End With
'remove extra paragraph at end
With newDoc.Paragraphs.Last.Range
If Len(.Text) = 1 Then .Delete
End With
Next pageNum
End Sub
另一种方法是获取新文档作为原件的精确副本,并带有页面布局。
Sub SavePageToNewDocument(SourceDoc As Document, FirstPage As Long, LastPage As Long)
Dim pageNum As Long
Dim srcPage As Range
Dim footer As Range
Dim newDoc As Document
For pageNum = FirstPage To LastPage
Set srcPage = SourceDoc.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageNum)
srcPage.End = srcPage.GoTo(what:=wdGoToBookmark, Name:="\page").End
Set footer = srcPage.Sections(1).Footers(wdHeaderFooterPrimary).Range
'create new document from the saved version of source document to get page layout
Set newDoc = Documents.Add(SourceDoc.FullName)
newDoc.Content.Delete
'take across text and formatting
newDoc.Range.FormattedText = srcPage.FormattedText
'remove extra paragraph at end
With newDoc.Paragraphs.Last.Range
If Len(.Text) = 1 Then .Delete
End With
Next pageNum
End Sub
推荐阅读
- django - Django 创建和更新视图:外键字段
- c# - DateTime.AddHours 给出错误的输出和日期时间格式更改
- ios - 在 objc_object::release() 中崩溃
- java - 在java中实现以对象为参数的方法
- vector - 二进制补码向量 VHDL 的左移
- python - 合并两个镜像 Pandas 列
- google-cloud-platform - 达到配额限制之前的 StatusCode.RESOURCE_EXHAUSTED + 混乱的仪表板
- bitbucket - 将自定义链接添加到 Bitbucket 中的 pullrequest 页面
- ruby-on-rails - Rails 5.2 - 主动存储 - 太慢了?
- nginx - 如何阻止 nginx 将上游解析为 ip?