我想转换发票数据的 Microsoft excel 表并填充邮件合并模板,一旦合并,我需要拆分每张发票并将其保存为 pdf。
下面的代码可以满足我的要求,但将它们保存为 1、2、3 等。我想使用的名称是在文档上找到的发票号(每页的前 8 个字符,不包括标题)。
这就是我的代码现在的样子:
Sub BreakOnPage()
Selection.HomeKey Unit:=wdStory
' 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
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdStory
Selection.TypeBackspace
Selection.Delete Unit:=wdWord, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Dim strInvoiceNumber As String
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
Text:="^#^#^#^#^#^#^#^#"
.Forward = True
.MatchWildcards = False
.Execute
End With
' Defines the DocNum
strInvoiceNumber = Selection.Text
' Exports the document to a pdf file and saves in sequence starting at 1 and closes the word document without saving
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Users\MLock\Documents\MacrosDocs\" & strInvoiceNumber & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
' Move the selection to the next page in the document.
Application.Browser.Next
Next i
' ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
如何在此处设置 PDF 文档的名称?