【问题标题】:Word VBA: Convert Batch of Word Files to PDF with Name From Table Contents within Each DocWord VBA:将批量 Word 文件转换为 PDF,名称来自每个文档中的表格内容
【发布时间】: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"

标签: vba pdf ms-word


【解决方案1】:

试试:

Sub ConvertDocs2PDFs()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      .SaveAs FileName:=Split(.FullName, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多