473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine if Outlook Is Open - If Not, Open It

18 New Member
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.Applic ation") ' Determine if Outlook is open.

If Err <> 0 Then
Call Shell("C:\Progr am 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
6 21397
jbt007
40 New Member
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
jtgarrison
18 New Member
@jbt007
I placed the code in a module and put the msgbox IsAppRunning(Ou tlook) and go a Compile Error: ByRef argument type mismatch.
Any ideas?
Jul 16 '10 #3
jbt007
40 New Member
@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 "IsAppRunni ng" sub.

When you type "IsAppRunni ng(" 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
jtgarrison
18 New Member
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(Ou tlook) and I get the same error.
Jul 16 '10 #5
jbt007
40 New Member
Select "Insert","Modul e" 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 "Declaratio ns" section of the module (See Declarations.jp g attached) and then create a function for "IsAppRunni ng" (See IsAppRunning.jp g attachment). After saving and compiling the code, in the immediate window you should be able to type ?IsAppRunning(O utlook) 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, 882 views)
File Type: jpg IsAppRunning.jpg (18.2 KB, 821 views)
File Type: jpg ImmediateWindow.jpg (16.6 KB, 769 views)
Jul 16 '10 #6
ADezii
8,834 Recognized Expert Expert
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
7965
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 take a print out. Can anyone help me out ? Thanks in advance for any help / guideline
3
6567
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 fine if the MS Outlook is open, but if the MS Outlook is closed, the program is unable to send more than 4 or 5 e-mails. I wish to add a...
3
4980
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 Outlook application (and/or Calendar) may or may not be open when this sub runs.
10
2366
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
5130
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 = CreateObject("Outlook.Application") This blows up with the message "Automation error, the specified module could not be found." If I change it to
2
1502
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
2239
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 objOutlookMsg As Outlook.MailItem Dim objOutlookRecip As Outlook.Recipient Dim objOutlookAttach As Outlook.Attachment Set objOutlook =...
1
4382
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; CComPtr<_NameSpace> session; hr = application->GetNamespace(CComBSTR("MAPI"),&session.p); if(hr != S_OK) return hr; hr = application->get_Session(&session.p);...
2
3045
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 interface with MS Ecxhange or Outlook from a business application and add items to a user's calendar, task list, or at a minimum to set a follow up flag in...
6
10624
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 closed, data is saved to the database but the email is not sent. Here is the the code. If anyone has suggestions on how I can get Outlook to open...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.