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
6 21397
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. -
Public Enum OfficeAppName
-
' enums always start from zero, unless otherwise specified
-
' for demonstration purposes (on the worksheet), start from 1
-
Outlook = 1
-
PowerPoint = 2
-
Excel = 3
-
Word = 4
-
Publisher = 5
-
Access = 6
-
End Enum
-
-
Function IsAppRunning(appName As OfficeAppName) As Boolean
-
' check if a particular Office app is running
-
-
On Error GoTo NotRunning
-
-
Dim officeApp As Object
-
Dim appString As String
-
-
' assume true
-
IsAppRunning = True
-
-
' determine which app is being checked
-
Select Case appName
-
Case 1 ' Outlook
-
appString = "Outlook"
-
Case 2 ' PowerPoint
-
appString = "PowerPoint"
-
Case 3 ' Excel
-
appString = "Excel"
-
Case 4 ' Word
-
appString = "Word"
-
Case 5 ' Publisher
-
appString = "Publisher"
-
Case 6 ' Access
-
appString = "Access"
-
End Select
-
-
' try to hook into existing object
-
Set officeApp = GetObject(, appString & ".Application")
-
-
ExitProc:
-
Exit Function
-
-
NotRunning:
-
IsAppRunning = False
-
Resume ExitProc
-
-
End Function
To use, just type something like: - Sub TestGet()
-
MsgBox IsAppRunning(Outlook)
-
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.
@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?
@jtgarrison
When you copied the code, did you copy it into a new module? Starting just under "Option Compare Database"?
This should put - Public Enum OfficeAppName
-
' enums always start from zero, unless otherwise specified
-
' for demonstration purposes (on the worksheet), start from 1
-
Outlook = 1
-
PowerPoint = 2
-
Excel = 3
-
Word = 4
-
Publisher = 5
-
Access = 6
-
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.
This is what I have: - Option Compare Database
-
Public Enum OfficeAppName
-
' enums always start from zero, unless otherwise specified
-
' for demonstration purposes (on the worksheet), start from 1
-
Outlook = 1
-
PowerPoint = 2
-
Excel = 3
-
Word = 4
-
Publisher = 5
-
Access = 6
-
End Enum
-
-
Function IsAppRunning(appName As OfficeAppName) As Boolean
-
' check if a particular Office app is running
-
-
On Error GoTo NotRunning
-
-
Dim officeApp As Object
-
Dim appString As String
-
-
' assume true
-
IsAppRunning = True
-
-
' determine which app is being checked
-
Select Case appName
-
Case 1 ' Outlook
-
appString = "Outlook"
-
Case 2 ' PowerPoint
-
appString = "PowerPoint"
-
Case 3 ' Excel
-
appString = "Excel"
-
Case 4 ' Word
-
appString = "Word"
-
Case 5 ' Publisher
-
appString = "Publisher"
-
Case 6 ' Access
-
appString = "Access"
-
End Select
-
-
' try to hook into existing object
-
Set officeApp = GetObject(, appString & ".Application")
-
-
ExitProc:
-
Exit Function
-
-
NotRunning:
-
IsAppRunning = False
-
Resume ExitProc
-
-
End Function
And even in the immediate window, I type msgbox isAppRunning(Ou tlook) and I get the same error.
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)
ADezii 8,834
Recognized Expert Expert
The API makes this a relatively simple problem to solve: - 'Declaration for the API Function
-
Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" _
-
(ByVal strClassName As String, ByVal lpWindowName As Any) As Long
- Dim retVal As Variant
-
-
'Adjust if necessary
-
Const conPATH_TO_OUTLOOK As String = "C:\Program Files\Microsoft Office\OFFICE12\OUTLOOK.EXE"
-
-
-
If apiFindWindow(CStr("rctrl_renwnd32"), 0&) = 0 Then
-
MsgBox conPATH_TO_OUTLOOK & " is not running!"
-
'Outlook is not running, let's open it
-
retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus)
-
Else
-
'Outlook is running, I'll leave the rest up to you!
-
MsgBox conPATH_TO_OUTLOOK & " is running!"
-
End If
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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.
|
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)
|
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
| |
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..
|
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 =...
|
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);...
|
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...
|
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...
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
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
|
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...
| | |