ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check to see if Outlook is open Q (https://www.excelbanter.com/excel-programming/399279-check-see-if-outlook-open-q.html)

Sean

Check to see if Outlook is open Q
 
How would I check to see if Oulook is open and if it isn't open an
instance of it?

I ask in this NG as I have a routine in Excel that e-mails out a file
and it is within this code I wish to include this check/action

Thanks


Jim Cone

Check to see if Outlook is open Q
 

From the Office XP Developer code library...
' Because Outlook is a multi-use COM component, if Outlook is not running,
' initializing an object variable to work with the Application object creates a
' new hidden instance. But if Outlook is already running, initializing the object
' variable returns a reference to the running instance.
'---
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
With olApp
' Code to automate Outlook here.
End With
olApp.Quit
Set olApp = Nothing
'---

If Outlook is not installed, the above might get pretty interesting.<g
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"Sean"
wrote in message
How would I check to see if Oulook is open and if it isn't open an
instance of it?
I ask in this NG as I have a routine in Excel that e-mails out a file
and it is within this code I wish to include this check/action
Thanks


Sean

Check to see if Outlook is open Q
 
Thanks Jim, your code works good, however if I wanted to leave Outlook
open instead of closing it when my macro finished, how would I do that
I tried removing olApp.Quit, thinking this is what closes Outlook, but
it still closed regardless or possibly the option for Outlook to
remain open for 2 mins (so send/receive can work) before closing

I've also tested it with an instance of Outlook alraedy open and it
doesn't open a 2nd instance nor does it close the 1st instance, which
is what I'd want to happen

Thanks




Jim Cone

Check to see if Outlook is open Q
 

It may be open and running and you just can't see it...
When a new instance is opened, it is invisible (as the notes stated).
Look in Task Manager to confirm.
If you want to see it, use olApp.Visible = True
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"Sean"
wrote in message
Thanks Jim, your code works good, however if I wanted to leave Outlook
open instead of closing it when my macro finished, how would I do that
I tried removing olApp.Quit, thinking this is what closes Outlook, but
it still closed regardless or possibly the option for Outlook to
remain open for 2 mins (so send/receive can work) before closing

I've also tested it with an instance of Outlook alraedy open and it
doesn't open a 2nd instance nor does it close the 1st instance, which
is what I'd want to happen
Thanks




Chip Pearson

Check to see if Outlook is open Q
 
Try something like the following. It will latch onto an existing instance of
Outlook if one exists. Otherwise, it will create a new instance. If the code
creates a new instance, it will Quit that instance when done. An existing
instance is left running and undisturbed.

Sub AAA()
Dim OLKApp As Outlook.Application
Dim WeStartedIt As Boolean

On Error Resume Next
Set OLKApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OLKApp Is Nothing Then
Set OLKApp = CreateObject("Outlook.Application")
If OLKApp Is Nothing Then
' can't create app
' error mesage then exit
MsgBox "Can't Get Outlook"
Exit Sub
End If
WeStartedIt = True
Else
WeStartedIt = False
End If
''''''''''''''''''''''''''''''
' your code here
''''''''''''''''''''''''''''''
If WeStartedIt = True Then
OLKApp.Quit
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Sean" wrote in message
oups.com...
Thanks Jim, your code works good, however if I wanted to leave Outlook
open instead of closing it when my macro finished, how would I do that
I tried removing olApp.Quit, thinking this is what closes Outlook, but
it still closed regardless or possibly the option for Outlook to
remain open for 2 mins (so send/receive can work) before closing

I've also tested it with an instance of Outlook alraedy open and it
doesn't open a 2nd instance nor does it close the 1st instance, which
is what I'd want to happen

Thanks





Sean

Check to see if Outlook is open Q
 
On Oct 13, 5:14 pm, "Chip Pearson" wrote:
Try something like the following. It will latch onto an existing instance of
Outlook if one exists. Otherwise, it will create a new instance. If the code
creates a new instance, it will Quit that instance when done. An existing
instance is left running and undisturbed.

Sub AAA()
Dim OLKApp As Outlook.Application
Dim WeStartedIt As Boolean

On Error Resume Next
Set OLKApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OLKApp Is Nothing Then
Set OLKApp = CreateObject("Outlook.Application")
If OLKApp Is Nothing Then
' can't create app
' error mesage then exit
MsgBox "Can't Get Outlook"
Exit Sub
End If
WeStartedIt = True
Else
WeStartedIt = False
End If
''''''''''''''''''''''''''''''
' your code here
''''''''''''''''''''''''''''''
If WeStartedIt = True Then
OLKApp.Quit
End If
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)

"Sean" wrote in message

oups.com...



Thanks Jim, your code works good, however if I wanted to leave Outlook
open instead of closing it when my macro finished, how would I do that
I tried removing olApp.Quit, thinking this is what closes Outlook, but
it still closed regardless or possibly the option for Outlook to
remain open for 2 mins (so send/receive can work) before closing


I've also tested it with an instance of Outlook alraedy open and it
doesn't open a 2nd instance nor does it close the 1st instance, which
is what I'd want to happen


Thanks- Hide quoted text -


- Show quoted text -


Thanks guys, my problem is that Outlook closes before send/receive is
actioned, thus my mail remains in the Outbox, how do I delay the
closing of Outlook for say 60 seconds


Sean

Check to see if Outlook is open Q
 
On Oct 13, 5:09 pm, "Jim Cone" wrote:
It may be open and running and you just can't see it...
When a new instance is opened, it is invisible (as the notes stated).
Look in Task Manager to confirm.
If you want to see it, use olApp.Visible = True
--
Jim Cone
San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)

"Sean"
wrote in message
Thanks Jim, your code works good, however if I wanted to leave Outlook
open instead of closing it when my macro finished, how would I do that
I tried removing olApp.Quit, thinking this is what closes Outlook, but
it still closed regardless or possibly the option for Outlook to
remain open for 2 mins (so send/receive can work) before closing

I've also tested it with an instance of Outlook alraedy open and it
doesn't open a 2nd instance nor does it close the 1st instance, which
is what I'd want to happen
Thanks


Jim, I used olApp.Visible = True but it debugs on this line (object
doesn't support this object or method), do I have to set any
references?. I entered as-:

Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
With olApp
olApp.Visible=True
' Code to automate Outlook here.
End With
olApp.Quit
Set olApp = Nothing


Jim Cone

Check to see if Outlook is open Q
 

Sean,
It looks like I fed you some bogus information. From the "technet/scriptcenter"...
"...Note that this is different from other Office programs; in Word or Excel, for example, you create an instance of the Application
object and then set the Visible property to True. That's not how things are done in Outlook. Here we bind to a namespace and a
folder, then call the Display method to make our folder - and the rest of Outlook - visible...."
The rest of the post is here...
http://www.microsoft.com/technet/scr.../tips0614.mspx
Looks like you have some work to do. <g
Jim Cone


"Sean"
wrote in message
On Oct 13, 5:09 pm, "Jim Cone wrote:
It may be open and running and you just can't see it...
When a new instance is opened, it is invisible (as the notes stated).
Look in Task Manager to confirm.
If you want to see it, use olApp.Visible = True
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



Jim, I used olApp.Visible = True but it debugs on this line (object
doesn't support this object or method), do I have to set any
references?. I entered as-:

Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
With olApp
olApp.Visible=True
' Code to automate Outlook here.
End With
olApp.Quit
Set olApp = Nothing


Sean

Check to see if Outlook is open Q
 
I've got the following code which appears to work except for the
following issues:-

1) The Copy_Paste macro it calls produces an e-mail which remain's in
Outlooks outbox until I open the application manually. Thus it appears
the code below closes Outlook before send/receive is actioned, however
I do have the option checked in Outlook where it does a send/receive
on application close, so not sure why it doesn't go

2) Within my Task Manager, although there are no application shown,
there is an Outlook.exe process that remains, so if I open Outlook
manually after the code below is run I have 2 Outlook processes. Does
this cause an issues? Doesn't look right to me


Option Explicit
Sub Auto_Open()
Application.ScreenUpdating = False
Application.WindowState = xlMinimized

Dim OLKApp As Outlook.Application
Dim WeStartedIt As Boolean


On Error Resume Next
Set OLKApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OLKApp Is Nothing Then
Set OLKApp = CreateObject("Outlook.Application")
If OLKApp Is Nothing Then
' can't create app
' error mesage then exit
MsgBox "Can't Get Outlook"
Exit Sub
End If
WeStartedIt = True
Else
WeStartedIt = False
End If


Dim OkToCallMacro As Boolean
OkToCallMacro = False
Select Case Weekday(Date)
Case vbMonday To vbFriday
If Time = TimeSerial(8, 44, 0) _
And Time < TimeSerial(8, 46, 0) Then
OkToCallMacro = True
End If
Case Is = vbSaturday, vbSunday
If Time = TimeSerial(10, 18, 0) _
And Time < TimeSerial(10, 20, 0) Then
OkToCallMacro = True
End If
End Select

If OkToCallMacro Then
Call Copy_Paste

If Workbooks.Count = 1 Then
'only this workbook is open
ThisWorkbook.Save
'close the application
'(which will close thisworkbook)
Application.Quit
Else
ThisWorkbook.Close savechanges:=True
End If
End If

If WeStartedIt = True Then
OLKApp.Quit
End If


End Sub


Jim Cone

Check to see if Outlook is open Q
 

Sean,

Your questions should really be directed to an Outlook newsgroup.
I pretend to know something about Excel but Outlook is an undiscovered country.
Nevertheless...

Item 1 - review the technet link again - note that it says...
"...If Outlook isn't running, however, you'll need to call the Logon method
and log on before your script can do anything..."

Item 2 - set all object references to Nothing...
Set OLKApp = Nothing
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"Sean"
wrote in message
I've got the following code which appears to work except for the
following issues:-

1) The Copy_Paste macro it calls produces an e-mail which remain's in
Outlooks outbox until I open the application manually. Thus it appears
the code below closes Outlook before send/receive is actioned, however
I do have the option checked in Outlook where it does a send/receive
on application close, so not sure why it doesn't go

2) Within my Task Manager, although there are no application shown,
there is an Outlook.exe process that remains, so if I open Outlook
manually after the code below is run I have 2 Outlook processes. Does
this cause an issues? Doesn't look right to me

Option Explicit
Sub Auto_Open()
Application.ScreenUpdating = False
Application.WindowState = xlMinimized

Dim OLKApp As Outlook.Application
Dim WeStartedIt As Boolean

On Error Resume Next
Set OLKApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If OLKApp Is Nothing Then
Set OLKApp = CreateObject("Outlook.Application")
If OLKApp Is Nothing Then
' can't create app
' error mesage then exit
MsgBox "Can't Get Outlook"
Exit Sub
End If
WeStartedIt = True
Else
WeStartedIt = False
End If

Dim OkToCallMacro As Boolean
OkToCallMacro = False
Select Case Weekday(Date)
Case vbMonday To vbFriday
If Time = TimeSerial(8, 44, 0) _
And Time < TimeSerial(8, 46, 0) Then
OkToCallMacro = True
End If
Case Is = vbSaturday, vbSunday
If Time = TimeSerial(10, 18, 0) _
And Time < TimeSerial(10, 20, 0) Then
OkToCallMacro = True
End If
End Select

If OkToCallMacro Then
Call Copy_Paste

If Workbooks.Count = 1 Then
'only this workbook is open
ThisWorkbook.Save
'close the application
'(which will close thisworkbook)
Application.Quit
Else
ThisWorkbook.Close savechanges:=True
End If
End If

If WeStartedIt = True Then
OLKApp.Quit
End If

End Sub



All times are GMT +1. The time now is 09:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com