Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-29
14
Medium Priority
?
1,143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

610 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