【发布时间】:2021-04-29 14:50:34
【问题描述】:
我是 VBA 新手,并尝试编写可以将多页 word 文档另存为单独 PDF 的代码。
我不仅在文件名上苦苦挣扎,目前,它正在使用 Page no 保存,但我要求它是 Word 文档中的特定字段。
我已经添加了word doc的截图,文件名应该是“User ID”和“Reminder 1 (this is fixed value)”.doc
这是我的代码
Sub SaveAsSeparatePDFs()
Dim I As Long
Dim xDlg As FileDialog
Dim xFolder As Variant
Dim xStart, xEnd As Integer
On Error GoTo lbl
Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xDlg.Show <> -1 Then Exit Sub
xFolder = xDlg.SelectedItems(1)
xStart = CInt(InputBox("Select Start Page No", "Information"))
xEnd = CInt(InputBox("Select End Page No", "Information"))
If xStart <= xEnd Then
For I = xStart To xEnd
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
xFolder & "\Reminder 1" & I & ".pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportFromTo, From:=I, To:=I, Item:=wdExportDocumentContent, _
IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=False, UseISO19005_1:=False
Next
End If
Exit Sub
lbl:
MsgBox "Enter right page number", vbInformation, "ERROR"
End Sub
【问题讨论】: