按名称引用文件夹

时间:2012-06-22 07:21:45

标签: outlook-vba outlook-2010

我需要按名称获取文件夹,而不是按文件夹编号计数。我尝试了各种方法。

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
'Dim OlFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.Folder
Dim myolItems As Outlook.Items
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
'Set myOlItems = objNS.GetDefaultFolder(37).Folders("Vijay Baswal").Items
'Open the folder
Set objFolder = olApp.Session.GetDefaultFolder("Vijay Baswal")

2 个答案:

答案 0 :(得分:3)

在收件箱下面说是一个名为Clients的文件夹,下面是一个名为Vijay Baswal的文件夹

设置objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders(“Clients”)。文件夹(“Vijay Baswal”)

OlDefaultFolders枚举http://msdn.microsoft.com/en-us/library/office/bb208072(v=office.12).aspx

收件箱是olFolderInbox或6.似乎没有37。

答案 1 :(得分:2)

请参阅以下vba代码段以查看如何从特定文件夹中读取邮件

 Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant
Dim outFolder As Outlook.Folder

 Dim olItem As Outlook.MailItem


Dim i As Long

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("folder1").Folders("fol2")
Set olItms = olFldr.Items


olItms.Sort "Subject"

i = 1

For Each olItem In olItms
    'If InStr(olMail.Subject, "Criteria") > 0 Then

       Dim szVar As String
      szVar = olItem.Body
        szVar1 = olItem.Subject
        i = i + 1
    'End If
Next olItem

Set olFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing