Excel VBA 用户表单以选择要从中复制的 Outlook 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10605206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:08:52  来源:igfitidea点击:

Excel VBA userform to Select Outlook folder to copy from

excelexcel-vbaoutlook-vbavba

提问by Johan Rheeder

I am trying to create a user form that will allow the user to select the folder to copy a set of emails from to an excel spreadsheet. I have done all the rest (ie created the copy process) but currently I have to manually enter the namespace and folder hierarchy for each new installation of this macro. Below is my manual process

我正在尝试创建一个用户表单,该表单将允许用户选择将一组电子邮件从中复制到 Excel 电子表格的文件夹。我已经完成了所有其余的工作(即创建了复制过程),但目前我必须为每个新安装的这个宏手动输入命名空间和文件夹层次结构。以下是我的手动过程

Set ol_App = New Outlook.Application
Set ol_Namespace = ol_App.GetNamespace("MAPI")
' Set ol_Folder = olNamespace.GetDefaultFolder(olFolderInbox)

' reference the folder that the emails are stored in
Set ol_Folder = ol_Namespace.Folders("Their own namespace")
Set ol_Folder = ol_Folder.Folders("Inbox")
Set ol_Folder = ol_Folder.Folders("Required_Folder")

Now this vba will be shared among a fair few people and each person has a different setup. Is there a way I can set this up in a userform using say a list-box and all they do is select the correct folder and click continue and the folder selection is stored in a variable or some sort?

现在这个 vba 将在少数人之间共享,每个人都有不同的设置。有没有一种方法可以使用列表框在用户表单中进行设置,他们所做的就是选择正确的文件夹并单击继续,然后将文件夹选择存储在变量或某种类型中?

Thank you in advance,

先感谢您,

回答by Siddharth Rout

Is this what you are trying? This will also negate the need to use a listbox. :)

这是你正在尝试的吗?这也将不需要使用列表框。:)

Option Explicit

'~~> Set a reference to Outlook Object x.x Library
Sub Sample()
    Dim oOlApp As Outlook.Application
    Dim objNmSpc As Namespace
    Dim ofldr As Object

    Set oOlApp = Outlook.Application
    Set objNmSpc = oOlApp.GetNamespace("MAPI")
    Set ofldr = objNmSpc.PickFolder

    If Not ofldr Is Nothing Then MsgBox ofldr
End Sub

And here is via Late Binding i.e, if you do not want to add the reference to Outlook Object x.x Library

这里是通过后期绑定,即,如果您不想添加对 Outlook 对象 xx 库的引用

Option Explicit

Sub Sample()
    Dim oOlApp As Object, objNmSpc As Object, ofldr As Object

    '~~> Establish an Outlook application object
    On Error Resume Next
    Set oOlApp = GetObject(, "Outlook.Application")

    If Err.Number <> 0 Then
        Set oOlApp = CreateObject("Outlook.Application")
    End If
    Err.Clear
    On Error GoTo 0

    Set objNmSpc = oOlApp.GetNamespace("MAPI")
    Set ofldr = objNmSpc.PickFolder

    If Not ofldr Is Nothing Then MsgBox ofldr
End Sub

EDIT:

编辑

SNAPSHOT

快照

enter image description here

在此处输入图片说明

回答by Scott Holtzman

What you want to do is loop through the Outlook Folders and have each Folder name populate the listbox /combobox in the activate event of the form. By doing that, as each person runs it, their own Outlook configuration will be what is used.

您想要做的是遍历 Outlook 文件夹,并让每个文件夹名称填充表单的激活事件中的列表框/组合框。通过这样做,当每个人运行它时,将使用他们自己的 Outlook 配置。

This link should give a good start Outlook Folder Loop

此链接应该给Outlook 文件夹循环一个良好的开端