Word VBA另存为PDF,可选储存位置,另存时可改名,PDF加密
时间: 2023-11-10 20:06:18 浏览: 187
WORD文档另存为PDF文件
5星 · 资源好评率100%
以下是一个Word VBA宏,可以实现另存为PDF并设置储存位置、改名和加密:
```vb
Sub SaveAsPDF()
Dim filePath As String
Dim fileName As String
Dim dlgSaveAS As FileDialog
Set dlgSaveAS = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
With dlgSaveAS
.Title = "另存为PDF"
.InitialFileName = ActiveDocument.FullName
.Filters.Clear
.Filters.Add "PDF 文件", "*.pdf"
.FilterIndex = 1
If .Show = -1 Then
filePath = .SelectedItems(1)
fileName = Mid(filePath, InStrRev(filePath, "\") + 1)
fileName = Left(fileName, Len(fileName) - 4) & ".pdf" '更改文件名为PDF格式
filePath = Left(filePath, InStrRev(filePath, "\")) & fileName
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
filePath, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, _
KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
'PDF加密设置
Set PDFSecurity = CreateObject("AcroExch.PDDoc")
PDFSecurity.Open (filePath)
PDFSecurity.SetSecurity "user password", "owner password", 0 '输入密码即可打开
PDFSecurity.Save 1, filePath
PDFSecurity.Close
MsgBox "保存成功!"
End If
End With
End Sub
```
这个宏会弹出一个文件保存对话框,让用户选择储存位置和文件名。保存为PDF文件后,会调用Acrobat的COM接口来加密PDF文件,输入密码后才能打开PDF文件。如果不需要加密,可以将PDF加密设置部分的代码删除。
阅读全文