• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

open outlook with excel vb only if there isn't one open

I want to open outlook, but i want ti to be in an if statement saying that if one is already open then don't do anytihng. I would also like my code to colse outlook at the end only if it opened an outlook though. Thanks.

I have this so far tro open it, but it does mutliple instances

Shell "OUTLOOK.EXE"
0
tchristie33
Asked:
tchristie33
  • 6
  • 6
  • 2
1 Solution
 
Rory ArchibaldCommented:
You can use something like this - the private bDoOutlookClose variable is True if you opened Outlook, False if it was already running:


Private bDoOutlookClose As Boolean
Private Function OpenOutlook() As Boolean
   'Use GetObject if an application is already running, if not, use CreateObject.
   'Error 429 occurs with GetObject if Outlook is not running.
   On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")
   
   If Err.Number = 429 Then 'Outlook is NOT running.
      Err.Number = 0
      bDoOutlookClose = True
      Set objOutlook = CreateObject("Outlook.Application") 'Create a new instance of Outlook
      If Err.Number = 429 Then 'MS Outlook is not installed.
         MsgBox "MS Outlook is not installed on your computer. ", vbOKOnly + vbCritical
         OpenOutlook = False
         Exit Function
      End If
   Else
      bDoOutlookClose = False
   End If
   OpenOutlook = True
End Function

Open in new window

0
 
merowingerCommented:
u can also use the wmi object win32_process and query for process.count
0
 
tchristie33Author Commented:
I want to keep it a lot simpler than that. Right now i can open and close outlook from excel vb, with this code

Sub test()
Shell "OUTLOOK.EXE"
outlook.Application.Quit
End Sub

Is there any way to see if outlook is running so that i can do something like this

Sub test()
if outlook.application = open then
msgbox it's open
Else
Shell "OUTLOOK.EXE"
outlook.Application.Quit
End if
End Sub
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tchristie33Author Commented:
or the oppisite, can i check and see if outlook is closed?
0
 
merowingerCommented:
0
 
Rory ArchibaldCommented:
How is that complicated? You just paste the function in, then in your code you do:

If OpenOutlook Then
   ' rest of your code
End If
If bDoOutlookClose Then objOutlook.Quit

0
 
tchristie33Author Commented:
I am not that advnaced with code yet. And the person for whom i am creating this for wants me to be able to expain everything. I can't explain all of those commands within the function. That is why i tried the other appraoch. I just figured that there has to be a short command that can see if outlook is closed or not or if it is running. If not i understand. Thanks for your time and help
0
 
Rory ArchibaldCommented:
This is the key bit of the function:

   On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")
   
   If Err.Number = 429 Then 'Outlook is NOT running.

you can use that on its own just to check if Outlook is already open.
Regards,
Rory
0
 
tchristie33Author Commented:
when i run the code below it doesn't catch the error caused by the line Set objOutlook = GetObject(, "Outlook.Application")



Sub test()
  On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")

   If Err.Number = 429 Then 'Outlook is NOT running.
            Shell "OUTLOOK.EXE"
    End If
'other code

outlook.Application.Quit

End Sub
0
 
Rory ArchibaldCommented:
What do you mean by "it doesn't catch the error"?
Also, note that your code is now about the same as the original code I posted in terms of complexity...
0
 
tchristie33Author Commented:
When i run it to see if out look is open and to get the object it doesn't alwasy register that as an erro even if outlook is closed. Also the code i am working with now does not deal with nay functions which makes it easy for me to understand and explain
0
 
Rory ArchibaldCommented:
To me that would imply that you may have invisible instances of Outlook running, hence no error. Did you check the process list in Task Manager?
0
 
tchristie33Author Commented:
when i originally run the code with no outlook open it works and opens outlook, and then when i run it again it closes it like it should. However, the problem comes when i run it once more, it does not register thtat command as being an error. I must first do some other stuf on my computer such as open and clsoe other windows and things like that, then wehn i run it again it works.
0
 
Rory ArchibaldCommented:
Try this version (you may need to reboot to clear out any existing instances of Outlook):

Sub OpenOutlook()
   Dim objOutlook As Object
   'Use GetObject if an application is already running, if not, use CreateObject.
   'Error 429 occurs with GetObject if Outlook is not running.
   On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")
   
   If Err.Number = 429 Then 'Outlook is NOT running.
      Err.Number = 0
      'Create a new instance of Outlook
      Set objOutlook = CreateObject("Outlook.Application")
   End If

   ' rest of your code here

   objOutlook.Quit
   Set objOutlook = Nothing
End Sub


Regards,
Rory
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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