Dim AcrobatApp As Object ' Acrobat.Application
Dim PDFDoc As Object ' Acrobat.Document
Dim FilePath As String
Dim OldPassword As String
Dim NewPassword As String
' パスワードを設定したい既存のPDFファイルのパスを指定
FilePath = "C:\フォルダ\既存のPDFファイル.pdf"
' 既存のPDFファイルのパスワード(設定されている場合)と新しいパスワードを指定
OldPassword = "" ' 既存のパスワードがない場合は空文字列を指定
NewPassword = "新しいパスワード"
' Acrobatアプリケーションを起動
Set AcrobatApp = CreateObject("AcroExch.App")
AcrobatApp.Show ' Acrobatのウィンドウを表示(不要な場合はコメントアウト)
' PDFを開く
Set PDFDoc = CreateObject("AcroExch.PDDoc")
If PDFDoc.Open(FilePath, OldPassword) Then
' パスワードを設定
If PDFDoc.SetSecurity(1, "", NewPassword, 0, "", "") Then ' 1: パスワード保護, 0: 印刷の制限なし
' パスワード付きで上書き保存
If PDFDoc.Save(1, FilePath) = False Then
MsgBox "PDFの保存に失敗しました。", vbExclamation
End If
' PDFを閉じる
PDFDoc.Close
Else
MsgBox "パスワードの設定に失敗しました。", vbExclamation
End If
Else
MsgBox "PDFファイルのオープンに失敗しました。", vbExclamation
End If
' Acrobatを終了
AcrobatApp.Exit
Set AcrobatApp = Nothing
MsgBox "PDFファイルにパスワードを付与しました。", vbInformation
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme