Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

application.quit doesn't...

14 views
Skip to first unread message

Andy Etherington

unread,
Jan 20, 1999, 3:00:00 AM1/20/99
to
I am trying to force outlook to quit from a VBA application in Excel. I
am using the following code:

Sub closingOutlook()
Dim myApp As Outlook.Application
On Error Resume Next
Set myApp = GetObject(, "outlook.application")
If Err.Number = 0 Then
Set olmapi = myApp.GetNamespace("MAPI")
Set folder = olmapi.GetDefaultFolder(olFolderInbox)
z8 = folder.Items.Count
While z8 > 0
emptyInbox
z8 = folder.Items.Count
Wend
Set folder = Nothing
Set olmapi = Nothing
myApp.Quit
Set myApp = Nothing
End If
ActiveCell.Value = "Closed Outlook at"
ActiveCell.Offset(0, 1).Value = Now()
ActiveCell.Offset(0, 2).Value = z8
ActiveCell.Offset(1, 0).Select

End Sub

It appears to work, reporting outlook closed, and all email deleted. My
problem comes in when the following code tries to open Outlook again:

Sub startOutlook()
Dim myApp As Outlook.Application
On Error Resume Next
Set myApp = GetObject(, "outlook.application")
If Err.Number <> 0 Then
Shell "C:\Program Files\Microsoft Office\Office\OUTLOOK.EXE"
ActiveCell.Value = "Opened Outlook at"
Else
ActiveCell.Value = "Outlook already open at"
If Not myApp.Visible Then myApp.Visible = True
End If
ActiveCell.Offset(0, 1).Value = Now()
ActiveCell.Offset(1, 0).Select
End Sub

Many times, the "Else" clause will be executed, even though Outlook
isn't visible (not in the task bar, not in the task list, nowhere). The
object returned from GetObject appears in the debug window as "Nothing".

Even odder is that, while the application (outlook) is not visible. it
is still taking email from the server...

Any clues?

Thanks

Andy Etherington

Harold S.

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to

>Set myApp = GetObject(, "outlook.application")
>If Err.Number = 0 Then

try:
If myApp = Nothing Then

Not sure this will work, I haven't tried it myself.

--
Harolds S.


Rick Spiewak

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
The symptoms you are seeing seem odd. Try using late binding
Instead of dim myApp as Outlook.Application, try:

dim myApp as Object
set myApp = CreateObject("Outlook.Application")

Andy Etherington wrote in message <36A616BD...@dhc.net>...


>I am trying to force outlook to quit from a VBA application in Excel. I
>am using the following code:
>
>Sub closingOutlook()
>Dim myApp As Outlook.Application
>On Error Resume Next

>Set myApp = GetObject(, "outlook.application")
>If Err.Number = 0 Then

> Set olmapi = myApp.GetNamespace("MAPI")
> Set folder = olmapi.GetDefaultFolder(olFolderInbox)
> z8 = folder.Items.Count
> While z8 > 0
> emptyInbox
> z8 = folder.Items.Count
> Wend
> Set folder = Nothing
> Set olmapi = Nothing
> myApp.Quit
> Set myApp = Nothing
>End If
>ActiveCell.Value = "Closed Outlook at"
>ActiveCell.Offset(0, 1).Value = Now()
>ActiveCell.Offset(0, 2).Value = z8
>ActiveCell.Offset(1, 0).Select
>
>End Sub
>
>It appears to work, reporting outlook closed, and all email deleted. My
>problem comes in when the following code tries to open Outlook again:
>
>Sub startOutlook()
>Dim myApp As Outlook.Application
>On Error Resume Next

>Set myApp = GetObject(, "outlook.application")

Daniel

unread,
Jan 29, 1999, 3:00:00 AM1/29/99
to
Andy,

Try deleting the "Set myApp = Nothing" statement. I've had the same
problem with my app in Word. I don't know for shure, but if you set your
reference to the outlook object to nothing, the container application
(in this case Excel) tries to "destroy" the object in its own weird way.
I can understand that you want to clean up memory after your program is
finished, but this can't be done in Windows. Once an instance of a
program has been loaded, it stays in memory for later use, even when you
don't see it in the task list.
I have a cute little utility, called "Process Viewer". With it, you can
see what's actually loaded into memory. If you want it, send me a mail.

Peace,
Daniel

Andy Etherington

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to Daniel
OK, I'm starting to get annoyed with Outlook. All I want it to do is
actually quit when I tell it to! I've tried all the suggestions made
thus far, and it still stays in memory after the "myApp.quit"
statement. It shows up in the process viewer that Danial sent
(Thanks!), as well as the box that ctrl-alt-del brings up, but I can't
access it any other way (that I've found, at least).

Any other clues?

Thanks.

Andy

HollisDPaulaliashdp

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to
Andy,

> myApp.Quit
> Set myApp = Nothing
>

Help in OL98 says the Quit method does everything; but you might try
a Namespaceobj.Logoff first. Athena has lectured us endlessly that
we should always Logoff and Exit, never just click the X box in the
upper right corner.


Hollis D. Paul; h...@csi.com
Sending from beautiful, midtown Mukilteo
4.02 build 244 (32-bit)for WG, Win95, Thu, 04 Feb 1999 08:54 PST

Harold S.

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to
In order for Outlook to end completely, all Outlook windows and tasks must
have ended. So make absolutely sure that this is true.

--
Harolds S.
Andy Etherington <aeth...@dhc.net> wrote in message
36B9BAD6...@dhc.net...

>> > myApp.Quit
>> > Set myApp = Nothing

Andy Etherington

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to
OK, I put the logoff statement in, and the first time I tried it, it
failed. Assuming that I was already in an invalid state with Outlook, I
then forced it to quit, and tried again. It worked fine. This is,
however, the same condition that I have had in the past: works fine a
couple of times, and then fails inexplicably.

I will check back and let you know when it fails or in a week, whichever
occurs first...

Andy.

HollisDPaulaliashdp wrote:
>
> Andy,


>
> > myApp.Quit
> > Set myApp = Nothing
> >

Andy Etherington

unread,
Feb 4, 1999, 3:00:00 AM2/4/99
to
Harold -

I can't see anything else that Outlook is running. It doesn't show up
in the task list (alt-TAB) as selectable. Any hints on how I can be
absolutely sure that it isn't trying to do something else?

Thanks.

Andy

> >> > myApp.Quit
> >> > Set myApp = Nothing

HollisDPaulaliashdp

unread,
Feb 5, 1999, 3:00:00 AM2/5/99
to
Andy,

> This is,
> however, the same condition that I have had in the past: works fine a
> couple of times, and then fails inexplicably.
>

Aren't computers wonderful!


Hollis D. Paul; h...@csi.com
Sending from beautiful, midtown Mukilteo

4.02 build 244 (32-bit)for WG, Win95, Thu, 04 Feb 1999 21:20 PST

Andy Etherington

unread,
Feb 5, 1999, 3:00:00 AM2/5/99
to
Yeah, except when my wife hears me cursing at the thing, and asks why
it's so much trouble. The only reply I can give her is "it's not a
Mac..."

Of course, I never tried the kinds of things I'm doing on this computer
on the Mac that we had, but it ran 24x7 for three years without a single
failure. I'm lucky if I can get through a day with a Windows machine
without a reboot...

Andy

Harold S.

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to
If you are running NT look at the Task Manager (right click the start bar
and select Task Manager...) or if you are using 95/98 you will need to find
a 3rd party tool to see all the services that are running. You have to make
sure that there are no Outlook services running, and when you use VB, VBA,
VBScript, Automation... Outlook can and will run as a service which does not
show up as a running application.

--
Harolds S.
Andy Etherington <aeth...@dhc.net> wrote in message

36B9F9D0...@dhc.net...

Andy Etherington

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to Daniel
Daniel -

While I'm not necessarily a Mac fan (I'm always suspicious of a mchine
that thinks it's smarter than I am, something that DOS NEVER
claimed...), I do appreciate something that actually works when asked to
do documented things. Since this is something that is SUPPOSED to work,
I will continue to pursue it, and blame MS for the effort.

To address your questions:

1. First a little background. Outlook is running on my work PC to keep
my external email server clean (sysadmin hates it when I let the stuff
accumulate, and I hate downloading mail that I've already seen). To try
to keep a little sanity, I am trying to run it only between the hours of
8AM and 6PM, Monday-Friday. That lets me check my email from home after
hours and on weekends.

Having said this, the two procedures could actually run in any order.
They are called from a third procedure, the scheduler, listed below,
anotated for clarity:

Sub scheduleOutlook()
booknme$ = ActiveWorkbook.Name 'save active workbook to return
Workbooks("Personal.xls").Activate
tme = Now()
j = Hour(tme)
dy = WeekDay(tme, vbMonday) 'using vbMonday means 6 or 7 is weekend

' I don't want to fire up Outlook on the weekend, just during the week.

If dy > 5 Then 'reschedule this procedure
Application.OnTime TimeValue("08:00:00"), "scheduleOutlook"
Else 'check whether we need to start or stop
If j < 18 Then 'must need to start
testStartOutlook
Application.OnTime TimeValue("18:00:00"), "scheduleOutlook"
Else 'Shut down outlook at 6PM
testClosingOutlook
Application.OnTime TimeValue("08:00:00"), "scheduleOutlook"
End If
End If

Workbooks(booknme$).Activate 'Return to starting place

End Sub

Now, Outlook is also in my Startup folder, so it is automatically
started on reboot of the computer. This means that it COULD already be
running when I start Excel (scheduleOutlook is run from AutoOpen in the
Personal.xls file).

2. As to why I used the shell command, I simply copied what was in the
shortcut in my Startup folder. It doesn't seem to matter, though.

As for you running the code in Excel, and it working, I can safely say
that I have done that too. It's the occasions that it STOPS working
that confound me. It will run for several days just fine, then one
time, just stop working. Once it stops working, it stops working until
I reboot. I don't get an error when I try the getObject, but it doesn't
return a real Outlook.Application object, either.

I have found an alternate way of accessing this phantom outlook, though:
if I click on the mail icon in the system tray, it will bring up the
application in a window, but it appears to be totally confused regarding
status (i.e. doesn't display the number of items in any of the mail
boxes, even though it is actively checking and cleaning the mailboxes).

Still searching for clues...

Andy

Daniel wrote:
>
> Andy,
>
> I'm a big Mac fan myself, so I decided to help you all the way. If you
> are not ready to give up on the problem, that is.
> I ran your code in Excel, and of course I didn't get the problem. In
> order to re-create the problem I have to know some things:
>
> 1. In what order are the two procedures (ClosingOutlook and
> StartOutlook) executed? I assume that these are extracts from your
> original code, so maybe you could provide me with a more elaborate code
> listing?
>
> 2. Why are you using the Shell command to start up Outlook (instead of
> CreateObject/GetObject)? I myself use GetObject to check if Outlook is
> already running. If this results in the "ActiveX can't create
> Object"-error, I use CreateObject to start Outlook from disk. So I
> really don't need to use the Shell command.
>
> By the way: it is not wise to use the "Resume Next"-clause while you are
> still developing the app. This works fine if you know what the error
> will be, but if there are errors you didn't anticipate, you don't get to
> see them.
>
> Peace of mind,
> Daniel
>

Daniel

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to aeth...@dhc.net
Andy,

I'm a big Mac fan myself, so I decided to help you all the way. If you
are not ready to give up on the problem, that is.
I ran your code in Excel, and of course I didn't get the problem. In
order to re-create the problem I have to know some things:

1. In what order are the two procedures (ClosingOutlook and
StartOutlook) executed? I assume that these are extracts from your
original code, so maybe you could provide me with a more elaborate code
listing?

2. Why are you using the Shell command to start up Outlook (instead of
CreateObject/GetObject)? I myself use GetObject to check if Outlook is
already running. If this results in the "ActiveX can't create
Object"-error, I use CreateObject to start Outlook from disk. So I
really don't need to use the Shell command.

By the way: it is not wise to use the "Resume Next"-clause while you are
still developing the app. This works fine if you know what the error
will be, but if there are errors you didn't anticipate, you don't get to
see them.

Peace of mind,
Daniel

Andy Etherington

unread,
Feb 16, 1999, 3:00:00 AM2/16/99
to Harold S.
Harold -

OK, I can do that from the keyboard. How do I do that from an
application, and more importantly, if Outlook IS running as a service,
how do I terminate it? It's interesting to note that, when it fails,
Outlook is in the task list, but GetObject returns no pointer to the
application. No error is generated (Err.Number is 0), the object is NOT
set to Nothing (so it points to SOMETHING), but it is NOT an Outlook
application object.

Any other ideas?

Thanks.

Andy Etherington

0 new messages