【问题标题】:Saving Word document as PDF将 Word 文档另存为 PDF
【发布时间】:2017-03-20 16:36:47
【问题描述】:

这与将 Word 文档转换为 PDF 格式有关。

我的 Excel VBA 代码出现错误。

运行时错误“5”:无效的过程调用或参数

保存到 Word 文档有效

objWord.ActiveDocument.SaveAs PathName & NewFileName & ".docx"

以下运行,但它会创建一个非常大的 PDF 文档。

objWord.ActiveDocument.SaveAs2 Filename:=PathName & NewFileName & ".pdf", _ 
  FileFormat:=wdFormatPDF

我在 Word 中录制了一个宏以将文件另存为 PDF,并按以下方式修改了生成的代码。

objWord.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
  PathName & NewFileName & ".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

我正在使用 Excel VBA 使用 Word 文档进行 Mailmerge,它工作正常并且能够以 Word 格式保存单个文档,但我需要将其保存为 PDF 格式。

【问题讨论】:

  • 您在哪一行代码中遇到了错误?

标签: excel vba ms-word


【解决方案1】:

试试这个代码,它确实将它保存在与 word 文档相同的文件夹中,但它可以工作。

    Private Sub Knop2_Click()
 Dim directory As String
 Dim enddirectory As String

    directory = "C:\docs" ' The starting directory
    enddirectory = "C:\pdf" 'einde

    Dim fso, newFile, folder, files
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(directory)
    Set files = folder.files

    For Each file In files


        Dim newName As String
        newName = Replace(file.Path, ".doc", ".pdf")
        newName = Replace(file.Path, ".docx", ".pdf")

        Documents.Open FileName:=file.Path, _
            ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
            wdOpenFormatAuto, XMLTransform:=""

        ActiveDocument.ExportAsFixedFormat OutputFileName:=newName, _
            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


    Next

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多