LoginSignup
0
1

More than 3 years have passed since last update.

【VBA】ExcelVBAでOutlookを操作するための基本

Posted at

まず、以下のとおり参照設定します。
Microsoft Outlook XX.X Object Library

基本的なコードは以下のとおりです。

Sub OutlookTest()

    Dim ol As Outlook.Application
    Set ol = New Outlook.Application

    Dim ns As Namespace
    Set ns = ol.GetNamespace("MAPI")

    '6番が受信トレイを示す
    Dim fl As MAPIFolder
    Set ns.GetDefaultFolder(6)

    '受信トレイのメールを受信日時で絞り込む
    Dim mails As Items
    Set mails = fl.Items.Restrict("[ReceivedTime] >= '" _
        & Format("2021/1/1 0:00", "ddddd h:nn AMPM") & "'" _
        & " And [ReceivedTime] < '" _
        & Format("2021/1/2 0:00", "ddddd h:nn AMPM") & "'")

    Dim m As MailItem
    For Each m In mails
        'メールに関する処理
    Next

    Set fl = Nothing
    Set ns = Nothing
    Set ol = Nothing

End Sub

ポイントはRestrictメソッドでメールを絞り込んでいるところです。
このように絞り込むことで、2021/1/1に受信したメールのみをmailsに格納しています。
日時の形式をFormat関数で整えているところ、シングルクォーテーションとダブルクォーテーションが入り乱れているところに注意してください。
日時以外にもいろいろな条件で絞り込みができるようです。

参考
Restrict メソッド (Outlook)

0
1
0

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