VBAでOutlook受信メール一覧をExcelに取り込みたいときはないでしょうか。
けど、そんな中で悩むことは、
・VBAでOutlookサブフォルダの受信メール一覧をExcelに取り込みたいが方法がわからない。
ですよね。
今回はそんなお悩みを解決する
・VBAでOutlook受信メールの添付ファイル一覧をExcelに取り込む方法
・VBAでOutlookサブフォルダの受信メール一覧をExcelに取り込む方法
についてまとめます!
もくじ
VBAでOutlook受信メール一覧をExcelに取り込むイメージ
VBAでOutlook受信メール一覧をExcelに取り込むイメージについて説明をします。
まず、Outlook側の受信トレイにあるメールを確認します。
取得したい期間を指定し、Excel側へVBAを実装、マクロを実行すると、
Excel側に受信トレイにあるメール一覧情報が取り込まれます!
さらに複数ある添付ファイル名を出力したり、

受信トレイのサブフォルダを指定したりもできます。
サブフォルダの出力結果です!

それでは早速やってみましょう!
Outlook受信メール一覧をExcelに取り込むVBA
VBAの準備
Outlook受信メール一覧をExcelに取り込むVBAの実装方法について説明をします。
今回のサンプルコードは以下の通りです。
Sub Outlook受信メール一覧を取り込む()
'Outlook用の定義
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object
'Excel用の定義
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim lnContactCount As Long
'取得結果を記述する行番号を指定します。2の場合、2行目のセルから開始されることになります。
lnContactCount = 2
'抽出期間の開始日と終了日を指定します。
strStart = Format("2022/2/16", "yyyy/mm/dd") '開始日を指定
strEnd = Format("2022/2/17", "yyyy/mm/dd") '終了日を指定
'スピードアップのためスクリーンの更新を無効にします。
Application.ScreenUpdating = False
'Excelのブックとワークシートのオブジェクトを設定します。
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)
'書き込み先のセルを指定し1行目にタイトルを入力します。
With wsSheet
.Cells.ClearContents
.Cells(1, 1).Value = "To"
.Cells(1, 2).Value = "CC"
.Cells(1, 3).Value = "BCC"
.Cells(1, 4).Value = "ReceivedTime"
.Cells(1, 5).Value = "Subject"
.Cells(1, 6).Value = "Body"
.Cells(1, 7).Value = "SenderName"
.Cells(1, 8).Value = "SenderEmailAddress"
.Cells(1, 9).Value = "SentOn"
.Cells(1, 10).Value = "ReceivedByName"
.Cells(1, 11).Value = "Importance"
.Cells(1, 12).Value = "Size"
.Cells(1, 13).Value = "CreationTime"
.Cells(1, 14).Value = "LastModificationTime"
.Cells(1, 15).Value = "ReminderTime"
.Cells(1, 16).Value = "BodyFormat"
.Cells(1, 17).Value = "EntryID"
'書式を追加します。
With .Range("A1:Z1")
.Font.Bold = True
.Font.ColorIndex = 10
.Font.Size = 11
End With
End With
wsSheet.Activate
'Outlookオブジェクトを設定し、MAPI名前空間を介してOutlookの連絡先一覧を取得します。
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
'既定ユーザーの受信トレイを対象にオブジェクトを取得します。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
Set olConItems = olFolder.Items
'Restrictメソッドで期間を指定し抽出するメールアイテムを絞り込みます。
Set olConItems = olConItems.Restrict("[ReceivedTime] >= '" & strStart & "' And [ReceivedTime] <= '" & strEnd & "'")
For Each olItem In olConItems
Debug.Print TypeName(olItem)
'アイテムのタイプが"MailItem"だった場合のみ値を取得します。※それ以外のタイプの場合はプロパティの構成が異なるためエラーとなります。
If TypeName(olItem) = "MailItem" Then
With olItem
Cells(lnContactCount, 1).Value = .To
Cells(lnContactCount, 2).Value = .CC
Cells(lnContactCount, 3).Value = .BCC
Cells(lnContactCount, 4).Value = .ReceivedTime
Cells(lnContactCount, 5).Value = .Subject
Cells(lnContactCount, 6).Value = .Body
Cells(lnContactCount, 7).Value = .SenderName
Cells(lnContactCount, 8).Value = .SenderEmailAddress
Cells(lnContactCount, 9).Value = .SentOn
Cells(lnContactCount, 10).Value = .ReceivedByName
Cells(lnContactCount, 11).Value = .Importance
Cells(lnContactCount, 12).Value = .Size
Cells(lnContactCount, 13).Value = .CreationTime
Cells(lnContactCount, 14).Value = .LastModificationTime
Cells(lnContactCount, 15).Value = .ReminderTime
Cells(lnContactCount, 16).Value = .BodyFormat
Cells(lnContactCount, 17).Value = .EntryID
End With
lnContactCount = lnContactCount + 1
End If
Next olItem
'オブジェクトを解放します。
Set olItem = Nothing
Set olConItems = Nothing
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
'スクリーンの更新を有効にします。
Application.ScreenUpdating = True
MsgBox "Outlook受信メールの取り込みが完了しました!", vbInformation
End Sub
VBAの設定
VBAの設定箇所は以下の通りです。
抽出期間の開始日と終了日を指定します。
strStart = Format("2022/2/16", "yyyy/mm/dd") '開始日を指定
strEnd = Format("2022/2/17", "yyyy/mm/dd") '終了日を指定
取得結果を記述する行番号を指定します。2の場合、2行目のセルから開始されることになります。
lnContactCount = 2
VBAの実装
「VBAの実装手順」をご参照ください。
VBAの実行
VBAを実行する前にOutlookの受信トレイをみてみましょう。
指定期間で3件の受信メールがあることが確認できます。
VBAを実行し
「Outlook受信メールの取り込みが完了しました!」と表示されることを確認します。
Excelシートを見てみましょう。
はい、受信メール一覧がExcelに取り込まれていますね!

VBAの説明
Excelのブックとワークシートのオブジェクトを設定します。
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)
書き込み先のセルを指定し1行目にタイトルを入力します。
.Cells(1, 1).Value = "To"
.Cells(1, 2).Value = "CC"
.Cells(1, 3).Value = "BCC"
.Cells(1, 4).Value = "ReceivedTime"
.Cells(1, 5).Value = "Subject"
.Cells(1, 6).Value = "Body"
.Cells(1, 7).Value = "SenderName"
.Cells(1, 8).Value = "SenderEmailAddress"
.Cells(1, 9).Value = "SentOn"
.Cells(1, 10).Value = "ReceivedByName"
.Cells(1, 11).Value = "Importance"
.Cells(1, 12).Value = "Size"
.Cells(1, 13).Value = "CreationTime"
.Cells(1, 14).Value = "LastModificationTime"
.Cells(1, 15).Value = "ReminderTime"
.Cells(1, 16).Value = "BodyFormat"
.Cells(1, 17).Value = "EntryID"
書式を追加します。
With .Range("A1:Z1")
.Font.Bold = True
.Font.ColorIndex = 10
.Font.Size = 11
End With
Outlookオブジェクトを設定し、MAPI名前空間を介してOutlookの連絡先一覧を取得します。
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
既定ユーザーの受信トレイを対象にオブジェクトを取得します。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
Set olConItems = olFolder.Items
Restrictメソッドで期間を指定し抽出するメールアイテムを絞り込みます。ReceivedTime(受信日)を基準にしています。
Set olConItems = olConItems.Restrict("[ReceivedTime] >= '" & strStart & "' And [ReceivedTime] <= '" & strEnd & "'")
アイテムのタイプが””MailItem””だった場合のみ値を取得します。※それ以外のタイプの場合はプロパティの構成が異なるためエラーとなります。
If TypeName(olItem) = "MailItem" Then
With olItem
Cells(lnContactCount, 1).Value = .To
Cells(lnContactCount, 2).Value = .CC
Cells(lnContactCount, 3).Value = .BCC
Cells(lnContactCount, 4).Value = .ReceivedTime
Cells(lnContactCount, 5).Value = .Subject
Cells(lnContactCount, 6).Value = .Body
Cells(lnContactCount, 7).Value = .SenderName
Cells(lnContactCount, 8).Value = .SenderEmailAddress
Cells(lnContactCount, 9).Value = .SentOn
Cells(lnContactCount, 10).Value = .ReceivedByName
Cells(lnContactCount, 11).Value = .Importance
Cells(lnContactCount, 12).Value = .Size
Cells(lnContactCount, 13).Value = .CreationTime
Cells(lnContactCount, 14).Value = .LastModificationTime
Cells(lnContactCount, 15).Value = .ReminderTime
Cells(lnContactCount, 16).Value = .BodyFormat
Cells(lnContactCount, 17).Value = .EntryID
End With
lnContactCount = lnContactCount + 1
End If
各プロパティの内容は以下の通りです。
プロパティ名 | 説明 |
---|---|
To | To表示名 |
CC | CC表示名 |
BCC | BCC表示名 |
ReceivedTime | 受信日時 |
Subject | 件名 |
Body | 本文 |
SenderName | 送信者名 |
SenderEmailAddress | 送信者メールアドレス |
SentOn | 送信日時 |
ReceivedByName | 受信者名 |
Importance | 重要度(2:高、0:低、1:中) |
Size | メールアイテム容量(バイト数) |
CreationTime | メールが作成された日時 |
LastModificationTime | メールを更新した日時 |
ReminderTime | リマインダーの日時 |
BodyFormat | メールの形式の種類(1:テキスト、2:HTML、,3:リッチテキスト) |
EntryID | EntryID(メールアイテム固有ID) |
添付ファイルを含むOutlook受信メール一覧をExcelに取り込むVBA
次に添付ファイルを含むOutlook受信メール一覧をExcelに取り込んでみましょう。
VBAの準備
サンプルコードは以下の通りです。
Sub Outlook受信メール一覧を取り込む()
'Outlook用の定義
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object
'Excel用の定義
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim lnContactCount As Long
'取得結果を記述する行番号を指定します。2の場合、2行目のセルから開始されることになります。
lnContactCount = 2
'抽出期間の開始日と終了日を指定します。
strStart = Format("2022/2/16", "yyyy/mm/dd") '開始日を指定
strEnd = Format("2022/2/17", "yyyy/mm/dd") '終了日を指定
'スピードアップのためスクリーンの更新を無効にします。
Application.ScreenUpdating = False
'Excelのブックとワークシートのオブジェクトを設定します。
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)
'書き込み先のセルを指定し1行目にタイトルを入力します。
With wsSheet
.Cells.ClearContents
.Cells(1, 1).Value = "To"
.Cells(1, 2).Value = "CC"
.Cells(1, 3).Value = "BCC"
.Cells(1, 4).Value = "ReceivedTime"
.Cells(1, 5).Value = "Subject"
.Cells(1, 6).Value = "Body"
.Cells(1, 7).Value = "SenderName"
.Cells(1, 8).Value = "SenderEmailAddress"
.Cells(1, 9).Value = "SentOn"
.Cells(1, 10).Value = "ReceivedByName"
.Cells(1, 11).Value = "Importance"
.Cells(1, 12).Value = "Size"
.Cells(1, 13).Value = "CreationTime"
.Cells(1, 14).Value = "LastModificationTime"
.Cells(1, 15).Value = "ReminderTime"
.Cells(1, 16).Value = "BodyFormat"
.Cells(1, 17).Value = "EntryID"
.Cells(1, 18).Value = "Attachments"
'書式を追加します。
With .Range("A1:Z1")
.Font.Bold = True
.Font.ColorIndex = 10
.Font.Size = 11
End With
End With
wsSheet.Activate
'Outlookオブジェクトを設定し、MAPI名前空間を介してOutlookの連絡先一覧を取得します。
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
'既定ユーザーの受信トレイを対象にオブジェクトを取得します。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
Set olConItems = olFolder.Items
'Restrictメソッドで期間を指定し抽出するメールアイテムを絞り込みます。
Set olConItems = olConItems.Restrict("[ReceivedTime] >= '" & strStart & "' And [ReceivedTime] <= '" & strEnd & "'")
For Each olItem In olConItems
Debug.Print TypeName(olItem)
'アイテムのタイプが"MailItem"だった場合のみ値を取得します。※それ以外のタイプの場合はプロパティの構成が異なるためエラーとなります。
If TypeName(olItem) = "MailItem" Then
With olItem
Cells(lnContactCount, 1).Value = .To
Cells(lnContactCount, 2).Value = .CC
Cells(lnContactCount, 3).Value = .BCC
Cells(lnContactCount, 4).Value = .ReceivedTime
Cells(lnContactCount, 5).Value = .Subject
Cells(lnContactCount, 6).Value = .Body
Cells(lnContactCount, 7).Value = .SenderName
Cells(lnContactCount, 8).Value = .SenderEmailAddress
Cells(lnContactCount, 9).Value = .SentOn
Cells(lnContactCount, 10).Value = .ReceivedByName
Cells(lnContactCount, 11).Value = .Importance
Cells(lnContactCount, 12).Value = .Size
Cells(lnContactCount, 13).Value = .CreationTime
Cells(lnContactCount, 14).Value = .LastModificationTime
Cells(lnContactCount, 15).Value = .ReminderTime
Cells(lnContactCount, 16).Value = .BodyFormat
Cells(lnContactCount, 17).Value = .EntryID
’添付ファイルの件数分添付ファイル名を出力します。
If .Attachments.Count > 0 Then
For j = 1 To .Attachments.Count
Cells(lnContactCount, 18).Value = Cells(lnContactCount, 18).Value & "," & .Attachments(j).Filename
Next
End If
End With
lnContactCount = lnContactCount + 1
End If
Next olItem
'オブジェクトを解放します。
Set olItem = Nothing
Set olConItems = Nothing
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
'スクリーンの更新を有効にします。
Application.ScreenUpdating = True
MsgBox "Outlook受信メールの取り込みが完了しました!", vbInformation
End Sub
VBAを実行
受信トレイに添付ファイル付きのメールがあることを確認しましょう。

2つの添付ファイルがあることが確認できます。
VBAを実行してみましょう。
はい、複数の添付ファイルでもきちんと出力されていますね。
VBAの説明
追加したコードは以下の通りです。
R列に”Attachments”の項目名を追加しました。
.Cells(1, 18).Value = "Attachments"
AttachmentsコレクションからItemオブジェクトを取り出し、そのFileNameプロパティからファイル名を取得しています。
If .Attachments.Count > 0 Then
For j = 1 To .Attachments.Count
Cells(lnContactCount, 18).Value = Cells(lnContactCount, 18).Value & "," & .Attachments(j).Filename
Next
End If
サブフォルダにあるOutlook受信メール一覧をExcelに取り込むVBA
最後にサブフォルダにあるOutlook受信メール一覧をExcelに取り込んでみましょう。
VBAの準備
サンプルコードは以下の通りです。
Sub Outlook受信メール一覧を取り込む()
'Outlook用の定義
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object
'Excel用の定義
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim lnContactCount As Long
'取得結果を記述する行番号を指定します。2の場合、2行目のセルから開始されることになります。
lnContactCount = 2
'抽出期間の開始日と終了日を指定します。
strStart = Format("2022/2/16", "yyyy/mm/dd") '開始日を指定
strEnd = Format("2022/2/17", "yyyy/mm/dd") '終了日を指定
'スピードアップのためスクリーンの更新を無効にします。
Application.ScreenUpdating = False
'Excelのブックとワークシートのオブジェクトを設定します。
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)
'書き込み先のセルを指定し1行目にタイトルを入力します。
With wsSheet
.Cells.ClearContents
.Cells(1, 1).Value = "To"
.Cells(1, 2).Value = "CC"
.Cells(1, 3).Value = "BCC"
.Cells(1, 4).Value = "ReceivedTime"
.Cells(1, 5).Value = "Subject"
.Cells(1, 6).Value = "Body"
.Cells(1, 7).Value = "SenderName"
.Cells(1, 8).Value = "SenderEmailAddress"
.Cells(1, 9).Value = "SentOn"
.Cells(1, 10).Value = "ReceivedByName"
.Cells(1, 11).Value = "Importance"
.Cells(1, 12).Value = "Size"
.Cells(1, 13).Value = "CreationTime"
.Cells(1, 14).Value = "LastModificationTime"
.Cells(1, 15).Value = "ReminderTime"
.Cells(1, 16).Value = "BodyFormat"
.Cells(1, 17).Value = "EntryID"
.Cells(1, 18).Value = "Attachments"
'書式を追加します。
With .Range("A1:Z1")
.Font.Bold = True
.Font.ColorIndex = 10
.Font.Size = 11
End With
End With
wsSheet.Activate
'Outlookオブジェクトを設定し、MAPI名前空間を介してOutlookの連絡先一覧を取得します。
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
'既定ユーザーの受信トレイを対象にオブジェクトを取得します。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("サブフォルダ")
Set olConItems = olFolder.Items
'Restrictメソッドで期間を指定し抽出するメールアイテムを絞り込みます。
Set olConItems = olConItems.Restrict("[ReceivedTime] >= '" & strStart & "' And [ReceivedTime] <= '" & strEnd & "'")
For Each olItem In olConItems
Debug.Print TypeName(olItem)
'アイテムのタイプが"MailItem"だった場合のみ値を取得します。※それ以外のタイプの場合はプロパティの構成が異なるためエラーとなります。
If TypeName(olItem) = "MailItem" Then
With olItem
Cells(lnContactCount, 1).Value = .To
Cells(lnContactCount, 2).Value = .CC
Cells(lnContactCount, 3).Value = .BCC
Cells(lnContactCount, 4).Value = .ReceivedTime
Cells(lnContactCount, 5).Value = .Subject
Cells(lnContactCount, 6).Value = .Body
Cells(lnContactCount, 7).Value = .SenderName
Cells(lnContactCount, 8).Value = .SenderEmailAddress
Cells(lnContactCount, 9).Value = .SentOn
Cells(lnContactCount, 10).Value = .ReceivedByName
Cells(lnContactCount, 11).Value = .Importance
Cells(lnContactCount, 12).Value = .Size
Cells(lnContactCount, 13).Value = .CreationTime
Cells(lnContactCount, 14).Value = .LastModificationTime
Cells(lnContactCount, 15).Value = .ReminderTime
Cells(lnContactCount, 16).Value = .BodyFormat
Cells(lnContactCount, 17).Value = .EntryID
’添付ファイルの件数分添付ファイル名を出力します。
If .Attachments.Count > 0 Then
For j = 1 To .Attachments.Count
Cells(lnContactCount, 18).Value = Cells(lnContactCount, 18).Value & "," & .Attachments(j).Filename
Next
End If
End With
lnContactCount = lnContactCount + 1
End If
Next olItem
'オブジェクトを解放します。
Set olItem = Nothing
Set olConItems = Nothing
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
'スクリーンの更新を有効にします。
Application.ScreenUpdating = True
MsgBox "Outlook受信メールの取り込みが完了しました!", vbInformation
End Sub
VBAを設定
Foldersコレクションにフォルダ名を指定しましょう。
受信トレイにサブフォルダが無い場合は作成し、添付ファイル付きのメール含めて受信メールを作成しましょう。
サンプルではフォルダ名が”サブフォルダ”であるのでFoldersコレクションにフォルダ名は”サブフォルダ”としています。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("サブフォルダ")
Set olConItems = olFolder.Items
VBAを実行する
VBAを実行しましょう。
はい、サブフォルダの受信メール一覧が出力されていますね。

VBAの説明
追加したコードは以下の通りです。
既定ユーザーの受信トレイにFoldersコレクションにフォルダ名を指定することによりサブフォルダを対象にオブジェクトを取得します。
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("サブフォルダ")
Set olConItems = olFolder.Items
VBAの実装手順
Excel VBAからOutlookを操作するための下準備
Excel VBAからOutlookを操作するための下準備をしていきます。
①Excelを起動し、「開発」タブをクリックします。
②VBEの画面が開いたら、メニューから「ツール」>「参照設定」を選択します。
③「Microsoft Outlook XX.X Object Library」を探してチェックボックスにチェックし「OK」をクリックします。
Excel側へVBAを実装する
Excel側にVBAを実装します。
①Excelを新規に開き、「開発」タブをクリックし、「VisualBasic」をクリックします。
もしくはショートカットキー「Alt」+「F11」でもOKです。
②標準モジュールを追加します。
左ペインのVBAProjectを右クリックし、「挿入」、「標準モジュール」を選択します。

③右ペインのウインドウに上記のマクロを入力します。
こちらで実装完了です。
VBAを実行する
VBAを実行する手順となります。
①「開発」タブの「VBA」をクリックし実行したいマクロを選択し、「実行」をクリックします。
②処理がされたことが確認できれば完了です。
さいごに
いかがでしょうか。
今回は、
VBAでOutlook受信メール一覧をExcelに取り込む方法!添付ファイルやサブフォルダも取得可!について
まとめました。
また、他にも便利な方法がありますので、よろしければご参照頂ければと思います。
コメントを残す