如何查看当前邮箱中的特定邮件?

How to check specific email in current email box?

我正在将特定电子邮件数据导出到 excel。 我面临的问题是,如果用户在用户表单中输入了错误的电子邮件标题,那么我的代码将因错误而停止“找不到 object” 如果用户在用户表单中输入错误的电子邮件标题并显示错误消息,我可以知道如何避免此错误吗?“这封电子邮件不在您当前的 Outlook 电子邮箱中或电子邮件不正确 title.Please 选择正确的电子邮箱或更正电子邮件标题“

Private Sub CommandButtonS_click()  'Declare outlook variables
Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem 'Declare excel variables
Dim xExcelApp As Excel.Application
Dim xWb As Excel.Workbook
Dim xWs As Excel.Worksheet 'Declare word variables
Dim Wordapp As Word.Application
Dim oLookWordDoc As Word.document
Dim oLookwordTbl As Word.Table
Dim iRow As Long 'row index
Dim EmailTitle As String
EmailTitle = Me.TextBox1.Text
If Application.ActiveExplorer.CurrentFolder.Items(EmailTitle) = "" Then
    MsgBox "This email is not in your current outlook email box Or incorrect email title.Please choose the correct email box Or correct the email title"
Exit Sub
Else
Set oLookMailitem = Application.ActiveExplorer.CurrentFolder.Items(EmailTitle)    'Grab the mail item
End If

您无法通过 Application.ActiveExplorer.CurrentFolder.Items(EmailTitle) 直接获取物品。

您可以使用 ActiveExplorer.CurrentFolder.Items(i) 一次浏览所有项目。

用于演示。这是最慢的方式,但足以满足“合理”数量的项目。
FindRestrict 更可取。

Option Explicit

Private Sub CommandButtonS_click()  'Declare outlook variables

Dim currFolder As folder
Dim oLookObject As Object
Dim oLookMailitem As mailItem

Dim EmailTitle As String

Dim i As Long
Dim foundFlag As Boolean

EmailTitle = "Test"

Set currFolder = ActiveExplorer.CurrentFolder

' This is the slowest way but sufficient for a folder with a "reasonable" number of items.
' Find or restrict is preferable.

For i = 1 To currFolder.Items.Count

    Set oLookObject = currFolder.Items(i)
    
    If oLookObject.Class = olMail Then
    
        Set oLookMailitem = oLookObject
        
        ' Now you may check for a mailitem property
        If oLookMailitem.subject = EmailTitle Then
            Debug.Print oLookMailitem.subject
            'oLookMailitem.Display
            foundFlag = True
            Exit For    ' Stop looking when item found.
        End If
    End If
    
Next

If foundFlag = False Then
    MsgBox "This email is not in your current outlook email box Or incorrect email title." & _
      vbCr & "Please choose the correct email box Or correct the email title."
End If

End Sub