使用 Excel VBA 选择 Outlook 文件夹 [英] Select Outlook Folder With Excel VBA

查看:32
本文介绍了使用 Excel VBA 选择 Outlook 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绕过必须选择我想要的文件夹,然后告诉 Excel 继续计算收件箱"

I'm trying to bypass having to select the folder I want and just tell Excel to go ahead and count the "Inbox"

Sub Get_Emails()

Dim OLF As Outlook.MAPIFolder
Dim EmailItemCount As Long

Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").PickFolder
EmailItemCount = OLF.Items.Count

Range("A1") = EmailItemCount

Set OLF = Nothing

Application.StatusBar = False

End Sub

有谁知道我如何无需选择文件夹就可以获得计数?Excel VBA 应该会自动进入收件箱"并给我计数.

Does anyone know how I can just get the count without having to select the folder? Excel VBA should just automatically go into the "Inbox" and give me my count.

注意:您必须转到工具">参考"> 并选择Microsoft Outlook 14.0 对象库"才能使此宏工作.

Note: You have to go to Tools > References > and select "Microsoft Outlook 14.0 Object Library" in order for this macro to work.

推荐答案

以下是有效的:

Option Explicit

Sub LoopFoldersInInbox()

    Dim ns              As Outlook.Namespace
    Dim myfolder        As Outlook.Folder
    Dim mysubfolder     As Outlook.Folder

    Set ns = GetObject("", "Outlook.Application").GetNamespace("MAPI")

    Set myfolder = ns.GetDefaultFolder(olFolderInbox)

    For Each mysubfolder In myfolder.Folders
        Debug.Print mysubfolder.name
        Debug.Print mysubfolder.Items.Count
    Next mysubfolder

End Sub

一些

这是后期绑定,因此您无需明确引用 Outlook 库,代码将适用于更多用户:

Here is the late binding, thus you do not need to refer to the Outlook Library explicitly and the code would work on more users:

Option Explicit

Sub LoopFoldersInInbox()

    Dim ns                  As Object
    Dim objFolder           As Object
    Dim objSubfolder        As Object

    Set ns = GetObject("", "Outlook.Application").GetNamespace("MAPI")
    Set objFolder = ns.GetDefaultFolder(6) ' 6 is equal to olFolderInbox

    For Each objSubfolder In objFolder.Folders
        Debug.Print objSubfolder.name
        Debug.Print objSubfolder.Items.Count
    Next objSubfolder

End Sub

在这个后期绑定中,我使用了 6 而不是 olFolderInbox.

In this late binding, I have used 6 in stead of olFolderInbox.

如果您想要单元格中的结果,请使用以下代码:

If you want the results in the cells, use this code:

Option Explicit

Sub LoopFoldersInInbox()

    Dim ns                  As Object
    Dim objFolder           As Object
    Dim objSubfolder        As Object
    Dim lngCounter          As Long

    Set ns = GetObject("", "Outlook.Application").GetNamespace("MAPI")
    Set objFolder = ns.GetDefaultFolder(6) ' 6 is equal to olFolderInbox

    For Each objSubfolder In objFolder.Folders
        With ActiveSheet
            lngCounter = lngCounter + 1
            .Cells(lngCounter, 1) = objSubfolder.Name
            .Cells(lngCounter, 2) = objSubfolder.Items.Count
        End With

        Debug.Print objSubfolder.Name
        Debug.Print objSubfolder.Items.Count

    Next objSubfolder

End Sub

这篇关于使用 Excel VBA 选择 Outlook 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆