Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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

0
paganji
Asked:
paganji
  • 3
1 Solution
 
Chuck WoodCommented:
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
0
 
Chris BottomleyCommented:
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

0
 
Chris BottomleyCommented:
Fixing the multiple views .. I think

Chris
Sub CheckIfOutlookIsRunning()
 
    Dim oOutlook As Object
    Dim oNameSpace As Object
    Dim oInbox As Object
    Dim expl As Variant
     
    On Error Resume Next
    Set oOutlook = CreateObject("Outlook.Application")
    If oOutlook.explorers.Count = 0 Then
       Set oNameSpace = oOutlook.GetNamespace("MAPI")
       Set oInbox = oNameSpace.GetDefaultFolder(6)
    
       Set expl = oOutlook.explorers.Add(oInbox, olFolderDisplayNoNavigation)
       expl.display
    End If
 
End Sub

Open in new window

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

Chris
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now