将Word文档另存为PDF [英] Saving Word document as PDF
问题描述
这与将Word文档转换为PDF格式有关.
This is related to converting a Word document to PDF format.
我的Excel VBA代码出现错误.
I get an error in my Excel VBA code.
运行时错误"5":无效的过程调用或参数
Run-time error "5": Invalid procedure call or argument
保存到Word文档中
objWord.ActiveDocument.SaveAs PathName & NewFileName & ".docx"
下面将运行,但是它将创建一个很大的PDF文档.
The below runs but it creates a PDF document which is very big in size.
objWord.ActiveDocument.SaveAs2 Filename:=PathName & NewFileName & ".pdf", _
FileFormat:=wdFormatPDF
我在Word中记录了一个宏,以将文件另存为PDF,并按如下所示修改了生成的代码.
I recorded a macro in Word to save the file as PDF and modified the generated code as per below.
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格式.
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.
推荐答案
尝试使用此代码,它确实将其保存在与单词文档所声明的位置相同的文件夹中,但是可以使用.
Try this code, it does save it in the same folder as where the word documents are stated but it works.
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
这篇关于将Word文档另存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!