Automating Outlook

  • Thread starter Thread starter Howard Kaikow
  • Start date Start date
H

Howard Kaikow

It is my understanding that Outlook is a single instance app, i.e., there
can be only a single instance of Outlook running.

I've been using code such as the following:

'Get existing instance of Outlook ; otherwise create a new one
' Outllok is a single instance application
On Error Resume Next
Set appOuttlook= GetObject(, "Outlook.Application")
If Err.Number = 0 Then
blnNew = False
Else
Set appOutlook= New Outlook.Application
blnNew = True
End If
On Error Goto 0

and then

With appOutlook
If blnNew Then
.Quit
Else
' Finish of macro, but leave Outlook running
End If
End With

However, I suspect that code may not be good enough.

If I created the NEW Outlook instance in code, how can I determine whether
the instance is not being by somebody outside of my code? Somebody could
have started Outlook session whilst my code is running, AFTER I created the
NEW Outlook?

Is the only choice to leave Outlook running?
 
Hi Howard,

AFAIK you don´t need to call Quit. Just set your variable to Nothing. If
nothing else holds another variable on OL, or if no user has started OL
manually then OL shuts down.
 
Michael Bauer said:
Hi Howard,

AFAIK you don´t need to call Quit. Just set your variable to Nothing. If
nothing else holds another variable on OL, or if no user has started OL
manually then OL shuts down.

I'm not sure about that.

I am concurrently investingating the same issue with Powerpoint.
A similar suggestion was made for PowerPoint, but does not work, i.e.,
unless I Quit
the PowerPoint instance in my code, setting to Nothing is not enough to kill
the critter.

I suspect Outlook works the same way.

For Powerpoint, I came up with the following.
I've posted in PowerPoint groups to see whethe rothers agree that the
solution is correct.

If correct, perhaps, the same approach would work with Outlook.
-----------------------------------
Came up with a "solution".
Seems too simple to be correct.

For the code below, one needs a Userform with 3 buttons and a listbox.

1. Shut down any running PowerPoint.
2. Click on the Create PPT button.
3. After the program creates a PPT session, create a PPT session via the
GUI, not using the icon minimized by the code.
4. Click on the Close PPT button.
5. Click on the Bye Bye button.

This code would appear to work for those cases in which my code need not
keep a presentation alive. So, if I close all the code's presentations, this
method appears to work.

If I have to keep a presentation alive, that should be no problem as I just
would not close PowerPoint, just set its object variable = Nothing.

Does this hanky panky solve the problem?

Option Explicit
Private appPPT As Powerpoint.Application
Private blnNewPPT As Boolean

Private Sub btnByeBye_Click()
Unload Me
End Sub

Private Sub btnClosePPT_Click()
With appPPT
If blnNewPPT Then
If .Presentations.Count = 0 Then
.Quit
lstActions.AddItem "New instance was Quit."
Else
lstActions.AddItem "New instance was NOT Quit."
End If
Else
On Error Resume Next
.ActivePresentation.Close
On Error GoTo 0
lstActions.AddItem "Extant instance was not Quit."
End If
End With
Set appPPT = Nothing
lstActions.AddItem "Set instance = Nothing."
On Error Resume Next
lstActions.AddItem Err.Number & ":" & Err.Description
On Error GoTo 0
End Sub

Private Sub btnCreatePPT_Click()
'Get existing instance of PowerPoint; otherwise create a new one
On Error Resume Next
Set appPPT = GetObject(, "PowerPoint.Application")
lstActions.AddItem Err.Number & ":" & Err.Description
If Err.Number = 0 Then
blnNewPPT = False
lstActions.AddItem "Extant instance is being used."
Else
Set appPPT = New Powerpoint.Application
blnNewPPT = True
lstActions.AddItem "New instance was created."
End If
With appPPT
.Visible = True
.WindowState = ppWindowMinimized
lstActions.AddItem "Presentations Count: " & .Presentations.Count
End With
On Error GoTo 0
End Sub
 
is there a way to detect how many folkes are referencing Outlook?

P.S.: I've been using the wrong terminology. MSFT uses "multi-use" and I've
been using "single use".
 
Michael Bauer said:
Howard,

why don´t you just test my suggestion?

Does not work, at least with PowerPoint.
First, I have to solve this for PPT.

To restate the problem.
----------------------------
I was using the wrong terminology.

PowerPoint is a Multi-use, single Instance app.

Assume that PPT is not running.

Then my code creates a NEW instance of PPT.
Then, while my code is running, PPT is again started by a user from the
desktop or via another piece of code.
Etc.

AFAIK, it is possible for all uses of PPT to have, at any point in time, a
Presentations.Count of 0, so I cannot tell whether I should Quit my instance
of PPT, based on Presentation count, lest it affect the other uses.

At some point, I would do the following:

1. DoEvents
2. Disable keyboard and mouse input
3. test for number of PowerPoint critters.
4. If only 1 critter, Quit PPT, otherwise let it live.
5. Enable keyboard and mouse input
6. exit from my code.

Enumerating processes, using say, the example at ALLAPI, lists a different
working set size for each use of PPT.

In my case, all I need to know is whether there is one use or more, does not
matter how many. If 1 use, then I can safely Quit my instance of Powerpoint.

I need to study the ALLAPI, and other examples, to make sure the info is
what I need.
 
Your terminology doesn´t matter in this case. You´re using the wrong
news group. My suggestion works for Outlook, I´ve never talked about
PowerPoint and can´t because that´s out off my area.
 
Michael Bauer said:
Hi Howard,

AFAIK you don´t need to call Quit. Just set your variable to Nothing. If
nothing else holds another variable on OL, or if no user has started OL
manually then OL shuts down.

I've still not tested your suggestion for Outlook, but the same behavior
does occur with PowerPoint, i.e., there is no need to ever Quit as
PowerPoint keeps track of things. Nor is there any need to set the object
variable = Nothing
 
Back
Top