Results 1 to 12 of 12
  1. #1
    JGrots is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jan 2013
    Posts
    65

    VBA Code for Opening Outlook if it is not already opened


    Hi,

    I'm trying to have a command button that will open Microsoft Outlook. When I click the button I want Outlook to pop-up and be the front screen. The code I'm using opens Outlook but it Outlook was already open it will open a new window of Outlook and each time I click the command button, a new version of Outlook will open. If I click the button 5 times, then I will have 5 Outlook windows opened. What is the code so that only one window of Outlook is opened at once?

    This is my code:

    Private Sub Command20_Click()
    On Error GoTo Err_Command20_Click

    Dim stAppName As String


    stAppName = "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"
    Call Shell(stAppName, 1)

    End If

    Exit_Command20_Click:
    Exit Sub

    Err_Command20_Click:
    MsgBox Err.Description
    Resume Exit_Command20_Click

    End Sub

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Full code and explanations here -
    http://www.databasejournal.com/featu...to-Outlook.htm

    Simpler version here -
    http://bytes.com/topic/access/answer...ss-send-mailto

    Of course, it partly depends on what you're using Outlook for. If you are just trying to send an email, you could use DoCmd.SendObject instead.

  3. #3
    JGrots is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jan 2013
    Posts
    65
    Dal,

    I'm only trying to open one instance of Outlook. I tried the first part of the code in the databasejounal but I couldn't get it to open. Do you know of any code that is like: "If Outlook is opened, then maximize window. If Outlook is not opened, then Open it"? I just don't want multiple windows opened of Outlook

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    This is adapted from that second link, assuming you don't want to close the copy of Outlook afterwards.

    Make sure that you also have set a reference in Access to the Microsoft Outlook XX.X Object library (XX.X is 14.0 for 2010, substitute the proper reference library for the Outlook you are using.)
    Code:
    Private Sub Command14_Click()
      
    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
      
    On Error Resume Next
      
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        'Outlook wasn't running, start it from code
        Set oOutlookApp = CreateObject("Outlook.Application")
        bStarted = True
    End If
      
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(olMailItem)
      
    With oItem
        'Set the recipient for the new email
       .To = "personname@company.com"
      
       .Display
    End With
      
    'If bStarted Then
    '    'If we started Outlook from code, then close it
    '    oOutlookApp.Quit
    'End If
      
    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
      
    End Sub
    In the above code after setting .To, you can set any of the other properties of the new MailItem object (.CC, .Subject, .Body, etc)
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

  5. #5
    JGrots is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jan 2013
    Posts
    65
    I tried your code but it doesn't work. Outlook won't open at all now.

  6. #6
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    1) What version of Access and outlook are you running?
    2) Make sure you added the reference to Outlook, not Office
    3) What error are you getting, on what line?

  7. #7
    JGrots is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jan 2013
    Posts
    65
    1) Access 2003 , Outlook 2010
    2)Microsoft Outlook 14.0 Object Library
    Click image for larger version. 

Name:	screenshot.jpg 
Views:	16 
Size:	89.5 KB 
ID:	13176
    3) I'm getting no errors. When I click the button, nothing happens. It opens when I use this code:
    stAppName = "C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.EXE"
    Call Shell(stAppName, 1)
    Attached Thumbnails Attached Thumbnails Screenshot.jpg  

  8. #8
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Okay, 1) add "Option Explicit" at the top of the module it's in.
    2) Comment out the "On Error Resume Next" and let it blow up so you get an error message.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Intersting. To use that code as is, you'd have to know the exact title of the window. At my work, the far left verbiage of the Outlook window title changes based on whether you're looking at your calendar or email or tasks, etc.

    It could be adapted, but if we can't get the code I posted to work for him as is, then we'll probably have to find him a different strategy. I've seen about five of them in twenty minutes of googling. The one I posted seemed the cleanest, and tested out on my machine.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    And I see that window title is reflected in Task Manager. Guess code would have to check for each one.

    I agree, the code you offer should work, always has for me regardless if Outlook is already open or closed, generates e-mail without opening multiple instances of Outlook. At least it does with Access 2007 at my office. I have done limited testing with 2010 on my home computer. Never tried with 2003.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    It appears that GetObject code is problematic in 2010 unless the Outlook is running in administrator mode:
    http://answers.microsoft.com/en-us/o...80000f8?page=1
    http://connect.microsoft.com/VisualS...vs2005-dev-env

    Although it's not even that simple - it looks like automating Outlook requires the calling program to be at the same level as the called program (admin vs normal user), at least in this report on Vista.
    http://social.msdn.microsoft.com/For...tlook-20072010

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need VBA code for adding to Outlook calendar
    By geraldk in forum Programming
    Replies: 3
    Last Post: 08-24-2012, 08:38 AM
  2. Replies: 3
    Last Post: 06-06-2012, 12:45 PM
  3. Replies: 1
    Last Post: 12-20-2011, 08:15 PM
  4. Error opening Outlook from Access
    By Toots in forum Programming
    Replies: 2
    Last Post: 03-13-2011, 06:00 PM
  5. Code needed when Database is Opened
    By Access_Headaches in forum Access
    Replies: 7
    Last Post: 08-13-2010, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums