Reputation: 77
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
Upvotes: 1
Views: 1943
Reputation: 66215
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
Upvotes: 2