LoginSignup
kento971017
@kento971017

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【VBA】フォルダの昨日00:00~23:59のメールを取得したい

解決したいこと

指定フォルダの昨日00:00~23:59のメールを取得してきたいのですが、当日の8:00以降だったり10:00以降だったりのメールしか取得できず、
日によって件数が前後してしまって安定しません。
0:00~23:59でメールのフィルタリングをするにはどうすればできますか?

以下にフィルタリングのコードも記載しているので、間違いがあればご指摘いただきたいです。

該当するソースコード

Public Sub SaveSelectedAsText()
    On Error Resume Next
    Dim dtStart As Date
    Dim dtEnd As Date
    Const TEXT_FILE = "C:Desktop\テスト.txt" ' 保存するファイル名を指定
    Dim strStart As String
    Dim strEnd As String
    Dim objOL As Object
    Dim objNAMESPC As Object
    Dim strFilter As String
    Dim myfolders As Object
    Dim objMail As MailItem
    Dim colItems As Items
    Dim objAttach As Attachment
    Dim strAttach As String

    dtStart = Date - 1
    dtEnd = Date - 1
    strStart = FormatDateTime(dtStart, vbShortDate)
     strEnd = FormatDateTime(dtEnd, vbShortDate)
    strFilter = "[受信日時] >= '" & strStart & _
                 "' AND [受信日時] <= '" & strEnd & " 23:59'"

     '保存したいメールフォルダを取得
    Set objOL = CreateObject("Outlook.Application")
    Set objNAMESPC = objOL.GetNamespace("MAPI")
    Set myfolders = objNAMESPC.Folders("kea@gmail.jp").Folders("追加")

    'メールフォルダをフィルタリング
    Set colItems = myfolders.Items.Restrict(strFilter)

    Open TEXT_FILE For Output As #1
    For Each objMail In colItems
        With objMail
            Print #1, "差出人:" & vbTab & .SenderName
            Print #1, "送信日時:" & vbTab & .SentOn
            If .To <> "" Then
                Print #1, "宛先:" & vbTab & .To
            End If
            If .CC <> "" Then
                Print #1, "CC:" & vbTab & .CC
            End If
            Print #1, "件名:" & vbTab & .Subject
            If .Attachments.Count > 0 Then
                strAttach = ""
                For Each objAttach In .Attachments
                    strAttach = strAttach & objAttach.Filename & "; "
                Next
                strAttach = Left(strAttach, Len(strAttach) - 2)
                Print #1, "添付ファイル: " & vbTab & strAttach
            End If
            If .Importance <> olImportanceNormal And .Sensitivity <> olNormal Then
                Print #1, ""
            End If
            If .Importance = olImportanceHigh Then
                Print #1, "重要度:" & vbTab & "高"
            End If
            If .Importance = olImportanceHigh Then
                Print #1, "重要度:" & vbTab & "低"
            End If
            If .Sensitivity = olConfidential Then
                Print #1, "秘密度:" & vbTab & "社外秘"
            End If
            If .Sensitivity = olPersonal Then
                Print #1, "秘密度:" & vbTab & "個人用"
            End If
            If .Sensitivity = olPrivate Then
                Print #1, "秘密度:" & vbTab & "親展"
            End If
            If .Categories <> "" Then
                Print #1, ""
                Print #1, "分類項目:" & vbTab & .Categories
            End If
            Print #1, ""
            Print #1, .Body
            Print #1, ""
        End With
    Next
    Close #1
End Sub

自分で試したこと

『"[受信日時] >= '" & strStart & "00:00'" & _』とすると、ColItemsがNothingとなってしまってうまくいきませんでした。

0

1Answer

Your answer might help someone💌