我正在尝试根据Excel工作表中列中提到的列表在Outlook中搜索邮件。下面是我写的VBA代码。但它不起作用。请帮忙:
Sub Test()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Dim rng As Range
Dim n As Variant
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder("Test1" as OlDefaultFolders)
'Set rng = [D3:D10]
n = Cells(4, 4).Value
'For Each n In rng
'i = Int(n)
'Cells(i, 10).Text = Cells(i, 4).Text
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "n") <> 0 Then
Cells(3, 4).Offset(0, 1).Value = "Y"
Else
Cells(3, 4).Offset(0, 1).Value = "N"
End If
Next olMail
'Next n
End Sub
答案 0 :(得分:0)
你不能将GetDefaultFolder用于任何东西,但是 - 默认文件夹(收件箱,联系人,日历等)。要获取特定文件夹,您需要知道文件夹的EntryID并使用NameSpace.GetFolderFromID或“遍历”特定根文件夹对象的文件夹集合(例如myInboxFolderObject.Folders(“MyInboxSubFolder”)。
另见“Enumerating, Searching, and Filtering Items in a Folder”。