【发布时间】:2014-08-19 23:06:08
【问题描述】:
我们收到一份包含发票的 Word 文档。文档可以是可变页数。文档中的每一页都代表一张发票。每页的最后一段包含一个唯一编号。这个唯一的号码总是以相同的三位数字开头,我们称之为“123”。整个数字的长度是可变的。
该宏应在每个分页符处创建一个新的 Word 文档,并根据该页面上的上述唯一编号为该新 Word 文档命名。发票永远不会超过一页。每个页面的页眉中都有一张图片,以及一个从单独的 Excel 文档中提取信息的链接字段。
我看过各种帖子。我无法让它们正确组合。我曾考虑将每个页面上的唯一编号放在表单域中并在每个页面上搜索表单域,但我无法构建任何远程关闭的内容。
我从以下代码开始:
Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage
For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy
' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "C:\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close
' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
简而言之,我需要以某种方式将.SaveAs FileName:=" 设置为:
- 查找页面上的最后一段并将其用作文件名。
- 查找以字符串开头的段落(例如:“123”),但该段落的长度可变。
我更喜欢选项 1,因为在某些情况下可能还有其他段落以“123”开头。
【问题讨论】: