使用 Excel VBA 将 Word 文档另存为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42909112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving Word document as PDF using Excel VBA
提问by Srinath
This is related to converting a Word document to PDF format.
这与将 Word 文档转换为 PDF 格式有关。
I get an error in my Excel VBA code.
我的 Excel VBA 代码出错。
Run-time error "5": Invalid procedure call or argument
运行时错误“5”:无效的过程调用或参数
Saving into a Word document works
保存为 Word 文档有效
objWord.ActiveDocument.SaveAs PathName & NewFileName & ".docx"
The below runs but it creates a PDF document which is very big in size.
下面运行,但它创建了一个非常大的 PDF 文档。
objWord.ActiveDocument.SaveAs2 Filename:=PathName & NewFileName & ".pdf", _
FileFormat:=wdFormatPDF
I recorded a macro in Word to save the file as PDF and modified the generated code as per below.
我在 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
I am using Excel VBA to do a Mailmerge using Word document and it is working fine and able to save individual documents in Word format but I need to save it in PDF format.
我正在使用 Excel VBA 使用 Word 文档进行邮件合并,它工作正常并且能够以 Word 格式保存单个文档,但我需要将其保存为 PDF 格式。
回答by Mirano Designs
Try this code, it does save it in the same folder as where the word documents are stated but it works.
试试这个代码,它确实将它保存在与 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