excel 将Word文档另存为PDF

velaa5lx  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(174)

这与将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文档,它工作正常,能够保存Word格式的个人文档,但我需要保存在PDF格式。

v64noz0r

v64noz0r1#

尝试这段代码,它确实保存在同一个文件夹中,因为那里的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

相关问题