0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBA エクセル OUTLOOKからメール内容を自動取得

Last updated at Posted at 2021-06-07

OUTLOOKからメール内容を自動取得

|A列|B列|C列|D列|E列|
|:---:|:---:|:---:|:---:|:---:|:---:|
|受信日時|送信元の名前|送信元のメールアドレス|件名|本文全て|

'多数の受信メールの情報を取得

Sub GetMultiMail()

    'Outlookアプリを参照
    Dim olApp As Outlook.Application
    Set olApp = New Outlook.Application
    
    'NameSpaceオブジェクトを取得
    Dim myNamespace As Outlook.Namespace
    Set myNamespace = olApp.GetNamespace("MAPI")
    
    '受信トレイの取得
    Dim myInbox As folder
    Set myInbox = myNamespace.GetDefaultFolder(olFolderInbox)
    
    '受信日時の降順にコレクションを並び替え
    Dim myItems As Outlook.Items
    Set myItems = myInbox.Items
    myItems.Sort "ReceivedTime", Descending:=True
    
    '最新の10件のメールを取得する
    Dim i As Long
    
    For i = 1 To 10
        
        '受信日時
        Cells(i + 1, 1).Value = myItems(i).ReceivedTime
        '送信元の名前
        Cells(i + 1, 2).Value = myItems(i).SenderName
        '送信元のメールアドレス
        Cells(i + 1, 3).Value = myItems(i).SenderEmailAddress
        '件名
        Cells(i + 1, 4).Value = myItems(i).Subject
        '本文全てを取得する場合
        Cells(i + 1, 5).Value = myItems(i).body
    
    Next i
    
    'オブジェクト変数の参照を無しにする。
    Set myNamespace = Nothing
    
    Set myInbox = Nothing
    
    Set olApp = Nothing

    
End Sub

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?