Reputation: 903
I'm trying to open the Outlook application from Access VBA when the switchboard loads. I've opened task manager and I can see an instance of Outlook appear for about 5 seconds then close, but I can't get the explorer window to open. I've been trying to piece together code from VBA: Determining whether an existing Outlook instance is open and other sources, but it's just not working. Any ideas?
And I would like to stick with late bindings so I don't have to worry about object libraries if someone opens with XP.
Function OpenEmail()
Dim olApp As Object ' Outlook.Application
Dim olFolderInbox As Object
Dim objExplorer As Object
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
MsgBox "Outlook is not Open"
Set olApp = CreateObject("Outlook.Application")
End If
Set objExplorer = CreateObject("Outlook.MAPIFolder")
Set objExplorer = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
objExplorer.Activate
'Set olApp = Nothing
End Function
Upvotes: 4
Views: 6539
Reputation: 149335
Outlook is the only MS Office application where GetObject
does the same thing as CreateObject
. Unlike other MS Office application, CreateObject
doesn't create multiple instances of Outlook.
Also olFolderInbox
is an outlook constant. You will have to define it in ACCESS.
Try this
Const olFolderInbox As Long = 6
Sub Sample()
Dim olApp As Object
Dim objNS As Object
Dim olFolder As Object
Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
With olFolder
'~~> Do what you want
End With
End Sub
Upvotes: 5