Check if outlook running in VBA

Peter.Stevens2

Board Regular
Joined
Sep 16, 2008
Messages
56
Hi,
I am trying to establish if Outlook 2000 is currently running in VBA , I have tried the following code but this only works when the user is actually positioned in "Inbox - Microsoft Outlook" etc. At this point I don't really care where/what the user has open in Outlook but merely to check if outlook is running.
I have tried below but does not work robustly enough.
I have also tried AppActivate("Microsoft Outlook")
I have also tried IsRunning("Outlook.Application") which does not even compile.
Code:
Dim OutlookErr, OutlookBox
On Error GoTo OutlookIsNotRunning
AppActivate ("outlook")
GoTo now_send_email

OutlookIsNotRunning:

OutlookErr = "Outlook is either not open or busy with another task." & vbCrLf & vbCrLf
OutlookErr = OutlookErr & "Please Open Outlook, Close any draft emails," & vbCrLf & vbCrLf
OutlookErr = OutlookErr & "the Global Address List or other activities and try again."
OutlookBox = MsgBox(OutlookErr, vbCritical, "Unable to access Outlook to send email")
Exit Sub
[COLOR=black]now_send_email: [/COLOR]

Thanks in advance for your help!

 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Something like:

Code:
Dim olApp As Object
 
On Error Resume Next
 
Set olApp = GetObject(,"Outlook.Application")
 
On Error Goto 0
 
If Not olApp Is Nothing Then
  'Outlook is open!
Else
  'Outlook not open
End if
 
Upvote 0
Hi Thanks for the reply.

Can anyone advise what objects I can examine from the GetObject ??

as I wish to use AppActivate("Outlook.Application")

Apparently the above will only work if the user has the inbox selected otherwise I was hope to supply AppActivate with the exact folder which the user has selected on so that the AppActivate works sucessfully.

Note : I have tried using AppActivate as above but if the user is not on the inbox then the AppActivate does not work.

Set myOlApp = GetObject(, "Outlook.Application")

AppActivate(myOlApp)

So is the above valid ? if not then what other objects can I extract from the GetObject item.

Thanks in advance.

I was hoping for maybe ? just maybe AppActivate(
 
Upvote 0
You could use:
Code:
Sub ActivateOL()
   '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.
      MsgBox "Outlook is not running"
   Else
      AppActivate objOutlook.ActiveExplorer.Caption
   End If
End Sub
 
Upvote 0
Thanks for the reply.

I am in a little tricy bind.

Code:
[COLOR=windowtext][FONT=Courier New][COLOR=windowtext][FONT=Courier New]Dim Msg, Style, Title, Help, Ctxt, Response, MyString, Managername
Dim OutlookErr, OutlookBox
Dim myOlApp             As Object
Dim olMailItem[/FONT][/COLOR]
Dim myMail              As Object<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New][/FONT][/COLOR] 
[COLOR=windowtext][FONT=Courier New]On Error Resume Next<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]Set myOlApp = GetObject(, "Outlook.Application")<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New] <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]On Error GoTo 0<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New] <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]If Not myOlApp Is Nothing Then<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    GoTo outlook_available_continue                 'Outlook is open!<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]Else<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    'Outlook NOT open<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    OutlookErr = "Outlook is not open - Please open outlook and re-try."<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    OutlookBox = MsgBox(OutlookErr, vbCritical,    Exit Sub<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]End If<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New] <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]outlook_available_continue:<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New] <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    Set myOlApp = CreateObject("Outlook.Application")<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    Set myMail = myOlApp.CreateItem(olMailItem)<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]          <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    With myMail<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        .Display<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        .To = ("test@rolls-royce.com") <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        .Subject = "WITH ATTACHMENTS emp_request"<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        .Body = "WITH ATTACHMENTS emp_request"<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        .Attachments.Add ThisWorkbook.FullName<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    End With<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    AppActivate (????) [/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    ‘???? Theres 2 opts here I could use myMail or myOlApp<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    ‘ONLY IF AUTO SEND NEEDED SendKeys ("^~")<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New] <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    Set myOlApp = Nothing<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    Set myMail = Nothing<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    Set olMailItem = Nothing<o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]        <o:p></o:p>[/FONT][/COLOR]
[COLOR=windowtext][FONT=Courier New]    MsgBox "Request Sent Successfully..."<o:p></o:p>[/FONT][/COLOR]
[FONT=Courier New][B]End Sub[/B][/FONT]

The problem is that the msgbox at the end advising the user that their email has been sent comes directly after the code that sends so consequently the macro display he msgbox leaving the newly create email somewhere in the taskbar which evades the user. The user who may have dozens of windows open then moves on thinking the email has been sent. It is not always viable to sendkeys after in case the users need to add recipients or signature.

Basically the problem is that I want the focus on the NEWLY CREATED email :confused: even if the msgbox after the createobject tries to grab focus.

What should I use for the AppActivate as using AppActivate (myOlApp.ActiveExplorer.Caption) simply sets focus to outlook and not the newly createdd email.

Any suggestions. Thanks in advance.
 
Upvote 0
Try changing ActiveExplorer to ActiveWindow, or use myMail.GetInspector.Caption instead.
 
Upvote 0
Thanks for that helped loads.

Just one thing finally. Both the AppActivate (myMail) and AppActivate (myMail.GetInspector.Caption) worked great thanks.

The only problem is that after the email is sent I would expect the active window to be the last one used before the send but it randonly goes to an explorer or some other window other than Excel.

I have tried to use AppActivate ("Microsoft Excel")
but this only stop the sendkeys from sending the email maybe due to timing issues. It seems placing any kind of activate after the sendkeys hinders the sendkeys.

What a Dilemma !!!!! any suggestions.

Code:
    With myMail
        .Display
        .To = ("[EMAIL="Kuldip.mond@rolls-royce.com"]Kuldip.mond@rolls-royce.com[/EMAIL]") ' "U9502056"
        .Subject = "WITH ATTACHMENTS emp_request_kuldip"
        .Body = "WITH ATTACHMENTS emp_request_kuldip"
        .Attachments.Add ThisWorkbook.FullName
    End With
    
    AppActivate (myMail)
    'AppActivate (myMail.GetInspector.Caption)
    
    SendKeys ("^~")
    
    'IF SENDKEYS ***NOT*** USED TO AUTO SEND EMAIL - COMENT the following code line,
    'as you want the focus to remain on the NEWLY Created email, not Excel to display MxgBox
    AppActivate ("Microsoft Excel")

Thanks again in advance
 
Upvote 0
If you want the mail sent automatically, don't use Display, use Send. You will probably have the Outlook security model to contend with though, in which case you can look at the Redemption library or an app like Click Yes. Sendkeys is pretty much never a good solution to a problem in my opinion for exactly this reason - you just don't have enough control over which window has the focus.
 
Upvote 0
Thanks a million. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I agree, the whole reason for using SendKeys was to stop the annoying Outlook security messages as they tend to be frustrating to users so the I'd rather let the users go back to excel (if not already focus) than getting outlook security trip them up all the time. <o:p></o:p>
<o:p></o:p>
Where can I find info on the Redemption library and app like Click Yes do you have a solution should I proceed down this relentless path... which I highly doubt but if it is a bullet proof solution then it might be worth while. Maybe ...

Still work great as it is with the AppActivate as above. Thanks again.<o:p></o:p>
 
Upvote 0
Info on Redemption can be found here. Not free for commercial use, but it works well.
ClickYes here - I have no personal experience of it, but lots of people I know recommend it.
You can program round it in C using Extended MAPI (which is basically what the Redemption library does for you) but that is beyond me.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,974
Members
448,537
Latest member
Et_Cetera

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top