Link to home
Start Free TrialLog in
Avatar of paganji
paganji

asked on

Open MS Outlook using VB

Hi Experts. I am using a MS Access database with VB. I wish to open outlook 2003 using code. I have attempted it using the following code but sometimes it works, and other times it gives an error saying "the operation failed" I cant explain why. Basically I only want it to open outlook if it is not open already. Thanks for any help.
Sub CheckIfOutlookIsRunning()
 
    Dim oOutlook As Object
    Dim oNameSpace As Object
    Dim oInbox As Object
     
    Set oOutlook = CreateObject("Outlook.Application")
    Set oNameSpace = oOutlook.GetNamespace("MAPI")
    Set oInbox = oNameSpace.GetDefaultFolder(6)
 
    oOutlook.Quit 'Close All Outlook copies if open
    oInbox.Display 'Open/show Outlook
 
End Sub

Open in new window

Avatar of Chuck Wood
Chuck Wood
Flag of United States of America image

It looks as though this line is closing Outlook before you display the Inbox. I am surprised it works at all.

oOutlook.Quit 'Close All Outlook copies if open
Avatar of Chris Bottomley
That was harder than I thought ... try the following.  If outlook is already open it will however open a new view on the folder.

Chris
Sub CheckIfOutlookIsRunning()
 
    Dim oOutlook As Object
    Dim oNameSpace As Object
    Dim oInbox As Object
    Dim expl As Variant
     
    Set oOutlook = CreateObject("Outlook.Application")
    Set oNameSpace = oOutlook.GetNamespace("MAPI")
    Set oInbox = oNameSpace.GetDefaultFolder(6)
 
    Set expl = oOutlook.explorers.Add(oInbox, olFolderDisplayNoNavigation)
    expl.display
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paganji
paganji

ASKER

Chris Bottomley, you truely are an expert. Many thanks for your help. The code worked perfectly.
Glad to help and thank you for the grade

Chris