如何使用Excel VBA在Outlook中指定嵌套文件夹 [英] How to specify nested folder in Outlook using Excel VBA
问题描述
我需要使用Excel VBA在Outlook中指定嵌套文件夹的帮助。我会发布我在下面使用的代码。
我可以指定收件箱文件夹,但是当我尝试指定收件箱文件夹中的文件夹时,该代码将返回没有这样的文件夹消息。有人知道为什么会发生这种情况吗?如果是这样,我该如何解决?
Option Explicit
Sub HowManyEmails()
Dim objOutlook As对象,objnSpace作为对象,objFolder As Object
Dim EmailCount As Integer
设置objOutlook = CreateObject(Outlook.Application)
设置objnSpace = objOutlook.GetNamespace(MAPI)
On Error Resume Next
设置objFolder = objnSpace.Folders(NoctalkSW)。文件夹(Inbox)
如果Err.Number<> 0然后
Err.Clear
MsgBox没有这样的文件夹。
Exit Sub
End If
EmailCount = objFolder.Items.Count
设置objFolder = Nothing
设置objnSpace = Nothing
设置objOutlook =没有
[B2] .Value = EmailCount
错误简历Next
设置objFolder =
objnSpace.Folders(NoctalkSW)。文件夹收件箱)。文件夹(COMPLETED)
如果Err.Number<> 0然后
Err.Clear
MsgBox没有这样的文件夹。
Exit Sub
End If
EmailCount = objFolder.Items.Count
设置objFolder = Nothing
设置objnSpace = Nothing
设置objOutlook =没有
[B3] .Value = EmailCount
End Sub
GetSharedDefaultFolder方法 返回一个MAPIFolder对象,表示指定的默认文件夹指定的用户。该方法用于委托方案,其中一个用户已经为其一个或多个默认文件夹的另一个用户授予了访问权限。
代码示例
Option Explicit
Const olFolderInbox = 6
Sub HowManyEmails()
Dim olApp As Object
Dim olNs As Object
Dim Inbox As Object
Dim SubFolder作为对象
Dim Recip As Object
设置olApp = CreateObject(Outlook.Application)
设置olNs = olApp.GetNamespace(MAPI)
设置Recip = olNs.CreateRecipient(0m3r@email.com)'共享地址
Recip.Resolve
设置收件箱= olNs.GetSharedDefaultFolder(Recip,olFolderInbox)'收件箱
[B2 ] .Value = Inbox.Items.Count
设置SubFolder = Inbox.Folders(COMPLETED)'子文件夹
[B3] .Value = SubFolder.Items.Count
设置olApp = Nothing
设置olNs = Nothin g
Set Inbox = Nothing
Set SubFolder = Nothing
Set Recip = Nothing
End Sub
I need help specifying a nested folder in Outlook using Excel VBA. I will post the code I'm using below.
I'm able to specify the "Inbox" folder but when I try and specify a folder that is within the "Inbox" folder, the code comes back with the "No such folder" message.
Does anyone know why this is happening to me? If so, how can I fix it?
Option Explicit
Sub HowManyEmails()
Dim objOutlook As Object, objnSpace As Object, objFolder As Object
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
On Error Resume Next
Set objFolder = objnSpace.Folders("NoctalkSW").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
[B2].Value = EmailCount
On Error Resume Next
Set objFolder =
objnSpace.Folders("NoctalkSW").Folders("Inbox").Folders("COMPLETED")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
[B3].Value = EmailCount
End Sub
If you wanna access shared Inbox and subfolder then Work with GetSharedDefaultFolder Method
GetSharedDefaultFolder Method Returns a MAPIFolder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders.
Code Example
Option Explicit
Const olFolderInbox = 6
Sub HowManyEmails()
Dim olApp As Object
Dim olNs As Object
Dim Inbox As Object
Dim SubFolder As Object
Dim Recip As Object
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Recip = olNs.CreateRecipient("0m3r@email.com") ' Share address
Recip.Resolve
Set Inbox = olNs.GetSharedDefaultFolder(Recip, olFolderInbox) ' Inbox
[B2].Value = Inbox.Items.Count
Set SubFolder = Inbox.Folders("COMPLETED") ' subfolder
[B3].Value = SubFolder.Items.Count
Set olApp = Nothing
Set olNs = Nothing
Set Inbox = Nothing
Set SubFolder = Nothing
Set Recip = Nothing
End Sub
这篇关于如何使用Excel VBA在Outlook中指定嵌套文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!