Solved

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

Posted on 2007-11-29
14
1,138 Views
Last Modified: 2009-12-16
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
Comment
Question by:tchristie33
  • 6
  • 6
  • 2
14 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20373576
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
 
LVL 31

Expert Comment

by:merowinger
ID: 20373694
u can also use the wmi object win32_process and query for process.count
0
 

Author Comment

by:tchristie33
ID: 20376710
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:tchristie33
ID: 20376786
or the oppisite, can i check and see if outlook is closed?
0
 
LVL 31

Expert Comment

by:merowinger
ID: 20377955
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20378595
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
 

Author Comment

by:tchristie33
ID: 20381362
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20381545
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
 

Author Comment

by:tchristie33
ID: 20382663
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20383135
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
 

Author Comment

by:tchristie33
ID: 20384804
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20384963
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
 

Author Comment

by:tchristie33
ID: 20385215
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 20398302
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question