View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Sean Sean is offline
external usenet poster
 
Posts: 454
Default 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