Excel VBA 的 Outlook.Namespace 上未定义用户定义类型?

User defined type not defined on Outlook.Namespace from Excel VBA?

我正在尝试从 Excel:

中搜索具有特定主题的 Outlook 电子邮件
Sub Work_with_Outlook()

    Set olApp = CreateObject("Outlook.Application")

    Dim olNs As Outlook.Namespace
    Dim Fldr As Outlook.MAPIFolder
    Dim olMail As Variant
    Dim sir() As String

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    Set myTasks = Fldr.Items

    Set olMail = myTasks.Find("[Subject] = ""*desired subject*""")
    If Not (olMail Is Nothing) Then
        sir = Split(olMail.Body, vbCrLf)
        For i = 1 To UBound(sir)
            ActiveWorkbook.Sheets("Sheet1").Cells(i, 1).Value = sir(i)
        Next i
        olMail.Delete
    End If

End Sub

我在 Excel 说

时出错

user defined type not defined

这一行:

Dim olNs As Outlook.Namespace

您需要在 VBA 项目中添加对 Outlook 的引用。

在VBA编辑器菜单栏点击工具 -> 参考并检查 Microsoft Outlook 14.0 对象库(版本号可能与 14.0 不同,具体取决于您的 MS Office 版本)。