[英]VBA Excel - Open a specific folder or subfolder in Outlook
I am trying to open outlook from a VBA macro in Excel.我正在尝试从 Excel 中的 VBA 宏打开 outlook。 I was able to open it but how do make it go to a specific folder?
我能够打开它,但如何让它转到特定文件夹? Let's say the "sent Items," "Draft folder," etc. Also, how about choosing a folder in another mailbox?
比方说“已发送邮件”、“草稿文件夹”等。另外,选择另一个邮箱中的文件夹怎么样? I have two mailboxes within my Outlook.
我的 Outlook 中有两个邮箱。
Here's the code that I have so far:这是我到目前为止的代码:
Sub my_prov_openOutlook()
Dim oOutlook As Object
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
If oOutlook Is Nothing Then
Shell ("OUTLOOK")
'''I would like to open a specific folder under the inbox or under a subfolder
Else
oOutlook.ActiveWindow.Activate
'''I would like to open a specific folder under the inbox or under a subfolder
End If
Do not use GetObject
with Outlook - use CreateObject
.不要将
GetObject
与 Outlook 一起使用 - 使用CreateObject
。 Outlook is a singleton: if it is already running, you will get a pointer to the existing instance. Outlook 是一个单例:如果它已经在运行,您将获得一个指向现有实例的指针。 If it is not running, it will start up.
如果它没有运行,它将启动。 Also don't use
On Error Resume Next
- you won't know if you get an error and where it occurs.也不要使用
On Error Resume Next
- 你不会知道你是否收到错误以及错误发生的位置。
Set oOutlook = CreateObject("Outlook.Application")
set oNS = oOutlook.GetNamespace("MAPI")
oNS.Logon 'does not do anything if Outlook is already running
set oFolder = oNS.GetDefaultFolder(6) 'olFolderInbox
if (oOutlook.ActiveExplorer Is Nothing) Then
oFolder.Display
Else
set oOutlook.ActiveExplorer = oFolder
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.