Outlook でメールと添付ファイルを1 つの PDF ファイルに変換・保存するには、どうすればよいですか?
この記事では、Outlook のメールメッセージとそのすべての添付ファイルを1 つの PDF ファイルとして保存する方法をご紹介します。
VBA コードでメールと添付ファイルを1 つの PDF ファイルに変換または保存する
VBA コードでメールと添付ファイルを1 つの PDF ファイルに変換または保存する
Outlook でメールとそのすべての添付ファイルを1 つの PDF ファイルとして保存するには、以下の手順に従ってください。
1。PDF ファイルにまとめて保存したい添付ファイル付きのメールを選択し、Alt+F11キーを押してMicrosoft Visual Basic for Applicationsウィンドウを開きます。
2。Microsoft Visual Basic for Applicationsウィンドウで、挿入 > モジュールをクリックします。その後、以下の VBA コードをモジュールウィンドウにコピーしてください。
VBA コード:メールと添付ファイルを1 つの PDF ファイルに保存
Public Sub MergeMailAndAttachsToPDF()
'Update by Extendoffice 2018/3/5
Dim xSelMails As MailItem
Dim xFSysObj As FileSystemObject
Dim xOverwriteBln As Boolean
Dim xLooper As Integer
Dim xEntryID As String
Dim xNameSpace As Outlook.NameSpace
Dim xMail As Outlook.MailItem
Dim xExt As String
Dim xSendEmailAddr, xCompanyDomain As String
Dim xWdApp As Word.Application
Dim xDoc, xNewDoc As Word.Document
Dim I As Integer
Dim xPDFSavePath As String
Dim xPath As String
Dim xFileArr() As String
Dim xExcel As Excel.Application
Dim xWb As Workbook
Dim xWs As Worksheet
Dim xTempDoc As Word.Document
On Error Resume Next
If (Outlook.ActiveExplorer.Selection.Count > 1) Or (Outlook.ActiveExplorer.Selection.Count = 0) Then
MsgBox "Please Select a email.", vbInformation + vbOKOnly
Exit Sub
End If
Set xSelMails = Outlook.ActiveExplorer.Selection.Item(1)
xEntryID = xSelMails.EntryID
Set xNameSpace = Application.GetNamespace("MAPI")
Set xMail = xNameSpace.GetItemFromID(xEntryID)
xSendEmailAddr = xMail.SenderEmailAddress
xCompanyDomain = Right(xSendEmailAddr, Len(xSendEmailAddr) - InStr(xSendEmailAddr, "@"))
xOverwriteBln = False
Set xExcel = New Excel.Application
xExcel.Visible = False
Set xWdApp = New Word.Application
xExcel.DisplayAlerts = False
xPDFSavePath = xExcel.Application.GetSaveAsFilename(InitialFileName:="", FileFilter:="PDF Files(*.pdf),*.pdf")
If xPDFSavePath = "False" Then
xExcel.DisplayAlerts = True
xExcel.Quit
xWdApp.Quit
Exit Sub
End If
xPath = Left(xPDFSavePath, InStrRev(xPDFSavePath, "\"))
cPath = xPath & xCompanyDomain & "\"
yPath = cPath & Format(Now(), "yyyy") & "\"
mPath = yPath & Format(Now(), "MMMM") & "\"
If Dir(xPath, vbDirectory) = vbNullString Then
MkDir xPath
End If
EmailSubject = CleanFileName(xMail.Subject)
xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & ".doc"
Set xFSysObj = CreateObject("Scripting.FileSystemObject")
If xOverwriteBln = False Then
xLooper = 0
Do While xFSysObj.FileExists(yPath & xSaveName)
xLooper = xLooper + 1
xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & "_" & xLooper & ".doc"
Loop
Else
If xFSysObj.FileExists(yPath & xSaveName) Then
xFSysObj.DeleteFile yPath & xSaveName
End If
End If
xMail.SaveAs xPath & xSaveName, olDoc
If xMail.Attachments.Count > 0 Then
For Each atmt In xMail.Attachments
xExt = SplitPath(atmt.filename, 2)
If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or (xExt = ".dotm") Or (xExt = ".dotx") _
Or (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or (xExt = ".xltm") Or (xExt = ".xltx") Then
atmtName = CleanFileName(atmt.filename)
atmtSave = xPath & Format(xMail.ReceivedTime, "yyyymmdd") & "_" & atmtName
atmt.SaveAsFile atmtSave
End If
Next
End If
Set xNewDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
Set xFilesFld = xFSysObj.GetFolder(xPath)
xFileArr() = GetFiles(xPath)
For I = 0 To UBound(xFileArr()) - 1
xExt = SplitPath(xFileArr(I), 2)
If (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or _
(xExt = ".xltm") Or (xExt = ".xltx") Then 'conver excel to word
Set xWb = xExcel.Workbooks.Open(xPath & xFileArr(I))
Set xTempDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
Set xWs = xWb.ActiveSheet
xWs.UsedRange.Copy
xTempDoc.Content.PasteAndFormat wdFormatOriginalFormatting
xTempDoc.SaveAs2 xPath & xWs.Name + ".docx", wdFormatXMLDocument
xWb.Close False
Kill xPath & xFileArr(I)
xTempDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
End If
Next
xExcel.DisplayAlerts = True
xExcel.Quit
xFileArr() = GetFiles(xPath)
'Merge Documents
For I = 0 To UBound(xFileArr()) - 1
xExt = SplitPath(xFileArr(I), 2)
If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or _
(xExt = ".dotm") Or (xExt = ".dotx") Then
MergeDoc xWdApp, xPath & xFileArr(I), xNewDoc
Kill xPath & xFileArr(I)
End If
Next
xNewDoc.Sections.Item(1).Range.Delete wdCharacter, 1
xNewDoc.SaveAs2 xPDFSavePath, wdFormatPDF
xNewDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
xWdApp.Quit
Set xMail = Nothing
Set xNameSpace = Nothing
Set xFSysObj = Nothing
MsgBox "Merged successfully", vbInformation + vbOKOnly
End Sub
Public Function SplitPath(FullPath As String, ResultFlag As Integer) As String
Dim SplitPos As Integer, DotPos As Integer
SplitPos = InStrRev(FullPath, "/")
DotPos = InStrRev(FullPath, ".")
Select Case ResultFlag
Case 0
SplitPath = Left(FullPath, SplitPos - 1)
Case 1
If DotPos = 0 Then DotPos = Len(FullPath) + 1
SplitPath = Mid(FullPath, SplitPos + 1, DotPos - SplitPos - 1)
Case 2
If DotPos = 0 Then DotPos = Len(FullPath)
SplitPath = Mid(FullPath, DotPos)
Case Else
Err.Raise vbObjectError + 1, "SplitPath Function", "Invalid Parameter!"
End Select
End Function
Function CleanFileName(StrText As String) As String
Dim xStripChars As String
Dim xLen As Integer
Dim I As Integer
xStripChars = "/\[]:=," & Chr(34)
xLen = Len(xStripChars)
StrText = Trim(StrText)
For I = 1 To xLen
StrText = Replace(StrText, Mid(xStripChars, I, 1), "")
Next
CleanFileName = StrText
End Function
Function GetFiles(xFldPath As String) As String()
On Error Resume Next
Dim xFile As String
Dim xFileArr() As String
Dim xArr() As String
Dim I, x As Integer
x = 0
ReDim xFileArr(1)
xFileArr(1) = xFldPath '& "\"
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
x = x + 1
xFile = Dir
Loop
ReDim xArr(0 To x)
x = 0
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
xArr(x) = xFile
x = x + 1
xFile = Dir
Loop
GetFiles = xArr()
End Function
Sub MergeDoc(WdApp As Word.Application, xFileName As String, Doc As Document)
Dim xNewDoc As Document
Dim xSec As Section
Set xNewDoc = WdApp.Documents.Open(filename:=xFileName, Visible:=False)
Set xSec = Doc.Sections.Add
xNewDoc.Content.Copy
xSec.PageSetup = xNewDoc.PageSetup
xSec.Range.PasteAndFormat wdFormatOriginalFormatting
xNewDoc.Close
End Sub 3。ツール>参照設定をクリックして、参照設定ダイアログボックスを開きます。Microsoft Excel オブジェクトライブラリ、Microsoft Scripting Runtime、およびMicrosoft Word オブジェクトライブラリのチェックボックスをオンにして、OKボタンをクリックしてください。スクリーンショットを参照してください:

4。F5キーを押すか、実行ボタンをクリックしてコードを実行します。名前を付けて保存ダイアログボックスが表示されたら、ファイルを保存するフォルダーを指定し、PDF ファイルに名前を付けて保存ボタンをクリックしてください。スクリーンショットを参照してください:

5。次に、Microsoft Outlookのダイアログボックスが表示されますので、OKボタンをクリックしてください。

これで、選択したメールとそのすべての添付ファイルが1 つの PDF ファイルとして保存されました。
注:この VBA スクリプトは、Microsoft Word およびExcel の添付ファイルにのみ対応しています。
Outlook で選択したメールをさまざまな形式のファイルとして簡単に保存できます:
一括保存ユーティリティ()Kutools for Outlook)を使えば、選択した複数のメールを、個別の HTML ファイル、テキストファイル、Word 文書、CSV ファイル、PDF ファイルとして、Outlook 内で以下スクリーンショットのようにカンタンに保存できます!Kutools for Outlook の無料版を今すぐダウンロード!

関連記事:
- Excel でコマンドボタンを使ってアクティブなワークシートを PDF ファイルとして保存するには、どうすればよいですか?
- ワークシートを PDF ファイルとして保存し、Outlook で添付ファイルとして送信するにはどうすればよいですか?
- Excel で選択範囲またはブック全体を PDF として保存するには、どうすればよいですか?
最高の Office 生産性ツール
まったく新しいKutools for Outlook を、100 以上の驚きの機能とともに体験しましょう!今すぐダウンロード!
🤖KUTOOLS AI:高度な AI 技術を活用して、メールの返信、要約、最適化、拡張、翻訳、作成など、あらゆる操作をラクラクこなします。
📧メール自動化:自動返信(POP および IMAP 対応)/メールのスケジュール送信/メール送信時にルールに基づいて自動 CC/BCC/自動転送(高度なルール)/自動で挨拶文を追加/複数の宛先を持つメールを個別のメッセージに自動分割...
📨メール管理:メールの取り消し/件名などを基準に詐欺メールをブロック/重複したメールを削除/高度な検索/フォルダーを整理...
📁添付ファイルプロ:一括保存/一括分離/一括圧縮/自動保存/自動的に切り離す/自動圧縮...
🌟インターフェースの魅力:😊さらに美しくクールな絵文字を多数収録/重要なメールが届いた際に通知/Outlook を閉じる代わりに最小化...
👍ワンクリックの驚き:全員に【Attachment】付きで返信/フィッシングメール対策/🕘送信者の現在時刻ゾーンを表示...
👩🏼🤝👩🏻連絡先とカレンダー:選択したメールから連絡先を追加を一括登録/連絡先グループを個別のグループに分割/誕生日のリマインダーを削除...
Kutools はお好みの言語でお使いいただけます!英語、スペイン語、ドイツ語、フランス語、中国語をはじめ、40 以上の言語をサポートしています!
ワンクリックでKutools for Outlook の機能を即解放!今すぐダウンロードして、効率を飛躍的にアップさせましょう!


🚀 ワンクリックダウンロード — Office アドインをすべて入手
強く推奨:Kutools for Office(5-in-1)
ワンクリックで5 つのインストーラーを 一括ダウンロード! ―Kutools for Excel、Outlook、Word、PowerPointおよびOffice Tab Pro。今すぐダウンロード!
- ✅ワンクリックで簡単操作:5 つのセットアップパッケージをたった1 回のクリックで一括ダウンロードできます。
- 🚀どんな Office 作業にも対応可能:必要なときに、必要なアドインをすぐインストールできます。
- 🧰含まれるもの:Kutools for Excel / Kutools for Outlook / Kutools for Word / Office Tab Pro / Kutools for PowerPoint