我正在尝试从Excel VBA计算Outlook“草稿”文件夹中的电子邮件数量。
我没有使用Outlook对象层次结构编程,所以我遇到了一些麻烦。
以下是我尝试过的其中一个变种。
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objNameSpace As Object
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set items = objNameSpace.GetDefaultFolder(olFolderDrafts).items
MsgBox items.count
当我运行此代码时,我收到错误:
运行时错误5无效的过程调用或参数
答案 0 :(得分:1)
我能够回答我自己的问题。首先,我没有点击MS Outlook对象库。其次,我将代码修改为以下内容:
Dim objOutlook As Object, objNameSpace As Object, objFolder As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
MsgBox objFolder.items.count
答案 1 :(得分:0)
您也可以这样做
Option Explicit
Public Const olFolderInbox As Long = 6
Public Const olFolderDrafts As Long = 16
Public Const olFolderContacts As Long = 10
Public Const olFolderDeletedItems As Long = 3
Public Sub Count_MailItems()
Dim olApp As Object ' Outlook.Application
Dim olNs As Object ' Outlook.Namespace
Dim Folder As Object ' Outlook.MAPIFolder
Dim Items As Object ' Outlook.Items
Dim Msg As String
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
' Set Folder = olNs.Session.PickFolder
'or
' Set Folder = olApp.ActiveExplorer.CurrentFolder
'or
Set Folder = olNs.GetDefaultFolder(olFolderInbox)
' olFolderDrafts
' olFolderContacts
' olFolderDeletedItems
Set Items = Folder.Items
Msg = Items.Count & " Items in " & Folder.Name & " Folder"
MsgBox (Msg)
End Sub