Outlookでメールと添付ファイルを単一のPDFファイルに変換または保存するにはどうすればよいですか?
この記事では、Outlookで電子メールメッセージとそれに含まれるすべての添付ファイルを単一のPDFファイルに保存する方法について説明します。
VBAコードを使用してメールと添付ファイルを単一のPDFファイルに変換または保存する
VBAコードを使用してメールと添付ファイルを単一のPDFファイルに変換または保存する
以下の手順に従って、Outlookでメールとそのすべての添付ファイルを単一のPDFファイルに保存してください。
1. 単一のPDFファイルに保存したい添付ファイル付きのメールを選択し、Alt + F11キーを押してMicrosoft Visual Basic for Applicationsウィンドウを開きます。
2. Microsoft Visual Basic for Applicationsウィンドウで、「挿入」>「モジュール」をクリックします。そして、以下のVBAコードをモジュールウィンドウにコピーします。
VBAコード:メールと添付ファイルを単一の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 Object Library」「 Microsoft Scripting Runtime」「 Microsoft Word Object Library」のチェックボックスをオンにして、「OK」ボタンをクリックします。スクリーンショットをご覧ください:

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

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

これで、選択したメールとそのすべての添付ファイルが単一のPDFファイルとして保存されました。
ノート: このVBAスクリプトは、Microsoft WordおよびExcelの添付ファイルにのみ有効です。
Outlookで選択したメールを簡単に異なる形式のファイルとして保存する方法:
Kutools for Outlookの「Bulk Save」機能を使用すると、複数の選択したメールを個別のHTML形式ファイル、TXT形式ファイル、Word文書、CSVファイル、またはPDFファイルとして簡単に保存できます(下のスクリーンショット参照)。今すぐKutools for Outlookの無料版をダウンロードしてください!

関連記事:
- Excelでコマンドボタンを使用してアクティブなワークシートをPDFファイルとして保存するにはどうすればよいですか?
- ワークシートをPDFファイルとして保存し、それを添付ファイルとしてOutlookでメール送信するにはどうすればよいですか?
- Excelで選択範囲または全体のワークブックをPDFとして保存するにはどうすればよいですか?
最高のオフィス生産性ツール
速報: Kutools for Outlook が無料版をリリース!
新しい Kutools for Outlook の無料版を体験してください。70以上の素晴らしい機能が永遠に使えます!今すぐダウンロードをクリック!
🤖 Kutools AI : 高度なAI技術を使用して、メールの返信、要約、最適化、拡張、翻訳、作成を簡単に行います。
📧 メール自動化: 自動返信 (POPとIMAPで利用可能) / メール送信のスケジュール / メール送信時にルールによる自動 CC/BCC / 自動転送 (高度なルール) / 自動挨拶追加 / 複数の宛先を持つメールを個別のメールに自動的に分割...
📨 メール管理: メールの取り消し / 件名やその他によるスパムメールのブロック / 重複したメールの削除 / 高度な検索 / フォルダーを整理...
📁 添付ファイルプロ: バッチ保存 / バッチ切り離し / バッチ圧縮 / 自動保存 / 自動的に切り離す / 自動圧縮...
🌟 インターフェースマジック: 😊より美しくクールな絵文字 /重要なメールが来たときに通知 / クローズ中ではなくOutlookを最小化...
👍 ワンクリックの驚き: 全員に【Attachment】付きで返信 / フィッシング対策メール / 🕘送信者のタイムゾーンを表示...
👩🏼🤝👩🏻 連絡先とカレンダー: 選択したメールから連絡先を一括追加 /連絡先グループを個別のグループに分割 / 誕生日のリマインダーを削除...
Kutools for Outlook をワンクリックで即座にアンロック。待たずに今すぐダウンロードして効率を高めましょう!

