简体   繁体   中英

VBA Excel - Open a specific folder or subfolder in Outlook

I am trying to open outlook from a VBA macro in Excel. 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.

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 . Outlook is a singleton: if it is already running, you will get a pointer to the existing instance. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM