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
try:
If myApp = Nothing Then
Not sure this will work, I haven't tried it myself.
--
Harolds S.
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")
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
Any other clues?
Thanks.
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
--
Harolds S.
Andy Etherington <aeth...@dhc.net> wrote in message
36B9BAD6...@dhc.net...
>> > myApp.Quit
>> > Set myApp = Nothing
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
> >
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
> 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
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
--
Harolds S.
Andy Etherington <aeth...@dhc.net> wrote in message
36B9F9D0...@dhc.net...
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
>
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
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