我正在尝试使用以下宏从Excel中搜索具有特定主题的电子邮件:
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中收到错误,说明此行未定义用户定义的类型:
Dim olNs As Outlook.Namespace
有人可以告诉我哪里出错了吗?谢谢,
答案 0 :(得分:1)
您需要在VBA项目中添加对Outlook的引用。
在VBA编辑器菜单栏中单击工具 - >引用并检查Microsoft Outlook 14.0对象库(版本号可能不同于14.0,具体取决于您的MS Office版本)。