【发布时间】:2018-10-23 18:30:38
【问题描述】:
尝试组合一个宏,将一批 word 文件转换为 PDF,文件名从每个 word 文件中的表格内容中提取。
我发现一个宏可以将打开的文档转换为具有正确文件名的 PDF,另一个宏可以将一批选定的 Word 文件转换为 PDF。
我无法“组合”它们以使 PDF 具有正确的文件名。任何帮助或建议将不胜感激!
Sub Open_File_To_PDF()
Dim StrFilename As String
Dim StrNm As String
Dim StrCat As String
StrNm = Split(ActiveDocument.Tables(1).Cell(5, 1).Range.Text, vbCr)(0)
StrCat = Split(ActiveDocument.Tables(1).Cell(2, 1).Range.Text, vbCr)(0)
StrFilename = StrCat & "_" & StrNm & ".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
StrFilename, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
Sub ConvertDocmInDirToPDF()
Dim filePath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
filePath = .SelectedItems(1)
End With
If filePath = "" Then Exit Sub
If Right(filePath, 1) <> "\" Then filePath = filePath & "\"
Application.ScreenUpdating = False
Dim currFile As String
currFile = Dir(filePath & "*.docm")
Do While currFile <> ""
Documents.Open (filePath & currFile)
Documents(currFile).ExportAsFixedFormat _
OutputFileName:=filePath & Left(currFile, Len(currFile) - Len(".docm")) & ".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
Documents(currFile).Close
currFile = Dir()
Loop
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
谢谢,@macropod!我无法弄清楚如何/在哪里插入命名规则。您对这些可能适合此宏的位置有任何建议吗?我尝试添加它,但不断收到错误消息。 Dim StrFilename As String Dim StrNm As String Dim StrCat As String StrNm = Split(ActiveDocument.Tables(1).Cell(5, 1).Range.Text, vbCr)(0) StrCat = Split(ActiveDocument.Tables(1). Cell(2, 1).Range.Text, vbCr)(0) StrFilename = StrCat & "_" & StrNm & ".pdf"