473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Determine if Outlook Is Open - If Not, Open It

Is there a way to tell if Outlook is open before sending a message and open it if it's not?

I have some old code that I have been using:

'Determine If Outlook Is Open. If Not, Open.
Set objOutlook = GetObject(, "Outlook.Application") ' Determine if Outlook is open.

If Err <> 0 Then
Call Shell("C:\Program Files\Microsoft Office\OFFICE12\OUTLOOK.EXE")
End If

But the problem is...is that even if Outlook is open, the code will open another instance of it, which is not ideally what I want.

Thanks in advance.

Jeff
Jul 16 '10 #1

✓ answered by ADezii

The API makes this a relatively simple problem to solve:
Expand|Select|Wrap|Line Numbers
  1. 'Declaration for the API Function
  2. Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" _
  3.  (ByVal strClassName As String, ByVal lpWindowName As Any) As Long
Expand|Select|Wrap|Line Numbers
  1. Dim retVal As Variant
  2.  
  3. 'Adjust if necessary
  4. Const conPATH_TO_OUTLOOK As String = "C:\Program Files\Microsoft Office\OFFICE12\OUTLOOK.EXE"
  5.  
  6.  
  7. If apiFindWindow(CStr("rctrl_renwnd32"), 0&) = 0 Then
  8.   MsgBox conPATH_TO_OUTLOOK & " is not running!"
  9.   'Outlook is not running, let's open it
  10.   retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus)
  11. Else
  12.    'Outlook is running, I'll leave the rest up to you!
  13.   MsgBox conPATH_TO_OUTLOOK & " is running!"
  14.  End If

6 21334
jbt007
40
Here is some sample code that does just that. Paste the following code at the top of a standard module in any Office program that supports VBA.

Expand|Select|Wrap|Line Numbers
  1. Public Enum OfficeAppName
  2. ' enums always start from zero, unless otherwise specified
  3. ' for demonstration purposes (on the worksheet), start from 1
  4.    Outlook = 1
  5.     PowerPoint = 2
  6.     Excel = 3
  7.     Word = 4
  8.     Publisher = 5
  9.     Access = 6
  10. End Enum
  11.  
  12. Function IsAppRunning(appName As OfficeAppName) As Boolean
  13. ' check if a particular Office app is running
  14.  
  15. On Error GoTo NotRunning
  16.  
  17. Dim officeApp As Object
  18. Dim appString As String
  19.  
  20. ' assume true
  21. IsAppRunning = True
  22.  
  23. ' determine which app is being checked
  24. Select Case appName
  25.     Case 1 ' Outlook
  26.        appString = "Outlook"
  27.     Case 2 ' PowerPoint
  28.        appString = "PowerPoint"
  29.     Case 3 ' Excel
  30.        appString = "Excel"
  31.     Case 4 ' Word
  32.        appString = "Word"
  33.     Case 5 ' Publisher
  34.        appString = "Publisher"
  35.     Case 6 ' Access
  36.        appString = "Access"
  37. End Select
  38.  
  39. ' try to hook into existing object
  40. Set officeApp = GetObject(, appString & ".Application")
  41.  
  42. ExitProc:
  43.   Exit Function
  44.  
  45. NotRunning:
  46.   IsAppRunning = False
  47.   Resume ExitProc
  48.  
  49. End Function
To use, just type something like:
Expand|Select|Wrap|Line Numbers
  1. Sub TestGet()
  2.   MsgBox IsAppRunning(Outlook)
  3. End Sub
It's not very sophisticated, but it works. You may want to beef up the exit code. On successful "GetObject", the officeApp will be the application that's running. So you might want to Set officeApp = nothing in the ExitProc.
Jul 16 '10 #2
@jbt007
I placed the code in a module and put the msgbox IsAppRunning(Outlook) and go a Compile Error: ByRef argument type mismatch.
Any ideas?
Jul 16 '10 #3
jbt007
40
@jtgarrison
When you copied the code, did you copy it into a new module? Starting just under "Option Compare Database"?
This should put
Expand|Select|Wrap|Line Numbers
  1. Public Enum OfficeAppName
  2. ' enums always start from zero, unless otherwise specified
  3. ' for demonstration purposes (on the worksheet), start from 1
  4.    Outlook = 1
  5.     PowerPoint = 2
  6.     Excel = 3
  7.     Word = 4
  8.     Publisher = 5
  9.     Access = 6
  10. End Enum
In the (declarations) section of the module. You will then have the rest of the code (starting with "Function...") in the "IsAppRunning" sub.

When you type "IsAppRunning(" you should get a drop-down list of "Outlook, PowerPoing, Excel, ..." when you type "(". Is that the case if you type it in the Immediate window?

The message box should pop up with "True" or "False" as the message.
Jul 16 '10 #4
This is what I have:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public Enum OfficeAppName
  3. ' enums always start from zero, unless otherwise specified
  4. ' for demonstration purposes (on the worksheet), start from 1
  5.    Outlook = 1
  6.     PowerPoint = 2
  7.     Excel = 3
  8.     Word = 4
  9.     Publisher = 5
  10.     Access = 6
  11. End Enum
  12.  
  13. Function IsAppRunning(appName As OfficeAppName) As Boolean
  14. ' check if a particular Office app is running
  15.  
  16. On Error GoTo NotRunning
  17.  
  18. Dim officeApp As Object
  19. Dim appString As String
  20.  
  21. ' assume true
  22. IsAppRunning = True
  23.  
  24. ' determine which app is being checked
  25. Select Case appName
  26.     Case 1 ' Outlook
  27.        appString = "Outlook"
  28.     Case 2 ' PowerPoint
  29.        appString = "PowerPoint"
  30.     Case 3 ' Excel
  31.        appString = "Excel"
  32.     Case 4 ' Word
  33.        appString = "Word"
  34.     Case 5 ' Publisher
  35.        appString = "Publisher"
  36.     Case 6 ' Access
  37.        appString = "Access"
  38. End Select
  39.  
  40. ' try to hook into existing object
  41. Set officeApp = GetObject(, appString & ".Application")
  42.  
  43. ExitProc:
  44.   Exit Function
  45.  
  46. NotRunning:
  47.   IsAppRunning = False
  48.   Resume ExitProc
  49.  
  50. End Function
And even in the immediate window, I type msgbox isAppRunning(Outlook) and I get the same error.
Jul 16 '10 #5
jbt007
40
Select "Insert","Module" from the VBA Editor, then paste the code in just under "Option Compare Database". When you do this, it should put the Enum in the "Declarations" section of the module (See Declarations.jpg attached) and then create a function for "IsAppRunning" (See IsAppRunning.jpg attachment). After saving and compiling the code, in the immediate window you should be able to type ?IsAppRunning(Outlook) and hit enter. When you hit the "(" you should get a drop-down list. (see Immediate.jpg also attached)
Attached Images
File Type: jpg Declarations.jpg (17.3 KB, 881 views)
File Type: jpg IsAppRunning.jpg (18.2 KB, 819 views)
File Type: jpg ImmediateWindow.jpg (16.6 KB, 768 views)
Jul 16 '10 #6
ADezii
8,834 Expert 8TB
The API makes this a relatively simple problem to solve:
Expand|Select|Wrap|Line Numbers
  1. 'Declaration for the API Function
  2. Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" _
  3.  (ByVal strClassName As String, ByVal lpWindowName As Any) As Long
Expand|Select|Wrap|Line Numbers
  1. Dim retVal As Variant
  2.  
  3. 'Adjust if necessary
  4. Const conPATH_TO_OUTLOOK As String = "C:\Program Files\Microsoft Office\OFFICE12\OUTLOOK.EXE"
  5.  
  6.  
  7. If apiFindWindow(CStr("rctrl_renwnd32"), 0&) = 0 Then
  8.   MsgBox conPATH_TO_OUTLOOK & " is not running!"
  9.   'Outlook is not running, let's open it
  10.   retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus)
  11. Else
  12.    'Outlook is running, I'll leave the rest up to you!
  13.   MsgBox conPATH_TO_OUTLOOK & " is running!"
  14.  End If
Jul 16 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Eager-Learner | last post by:
Through window.open or window.showmodaldialog, I want to open a word or excel document in Print Preview mode. Bcos I don't want the user to make any changes or save it but the user can ONLY VIEW OR...
3
by: Jordan | last post by:
I created and access application that sends e-mails automaticaly to our clients, over 1.500 e-mails daily. I am using the MS Outlook components to create the e-mail with HTML code. All works...
3
by: deko | last post by:
When adding Outlook Appointment Items from Access, should I use: Set ol = New Outlook.Application --or-- Set ol = CreateObject("Outlook.Application") Outlook seems to crash either way. The...
10
by: Gunnar G | last post by:
I'm having problem reading from the beginning of a file. Here is the code (more or less) ifstream codefin; ofstream codefout; while (not_annoyed)
9
by: salad | last post by:
I have the following code. I have a reference to the Outlook library in the reference list. My machine is standalone, not on a network. Dim Olapp As Outlook.Application Set Olapp =...
2
by: developing | last post by:
hello, i am sending an email from access. works fine if outlook is open, if not then KABOOM...how can i determine if outlook is open or not from vba? i am trying to trap that error..
9
by: ARC | last post by:
In case anyone has ran into this yet. The following code used to work with older versions of the MS Outlook library, but would error out in 2007: Dim objOutlook As Outlook.Application Dim...
1
by: vibhavleo | last post by:
I am using the Microsoft Office 11.0 Object library to connect to outlook. My code is as follows: _ApplicationPtr application("Outlook.Application"); CComPtr<_NameSpace> ns;...
2
by: =?Utf-8?B?RU1jQ2FydGh5?= | last post by:
My organization currently has interfaces in place to generate MS Outlook emails from our business applications. I am trying to determine if it is possible and how difficult it might be to...
6
by: dlblack | last post by:
I am using the following code to send emails with attachments from Access using Outlook. The code works fine if Outlook is already open and emails are sent as expected via Outlook. When Outlook is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.