Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is Outlook running in background? 3

Status
Not open for further replies.

DrSimon

IS-IT--Management
Dec 14, 2001
674
GB
I have a VBA application that is sending appointments to Outlook, but want to check if it's running in the background before I do so. My code below tells me if it's running but i can't find a property to give me what I want. Can anyone help please?

Code:
Function Outlook_is_Running()
    Dim MyOL As Object
    On Error Resume Next    ' Defer error trapping.
    ' Getobject function called without the first argument returns a
    ' reference to an instance of the application. If the application isn't
    ' running, an error occurs.
    Set MyOL = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Outlook_is_Running = False
        MyOL.Quit
    Else
        Outlook_is_Running = True
    End If
    Set MyOL = Nothing
End Function

Thanks

Simon Rouse
 
>to give me what I want

What do you want?

Incidentally MyOL.Quit is not required because no OutLook.Application was 'Got' in that case.
 
Thanks HughLerwill
Of course you're right about the Quit.
What I want is to determine if the Outlook process is open as a foreground or background task. I've found that even when apparently closed as a foreground task it is still running for a time in the background.

Simon
 
As Outlook is mono instance, what is your problem ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK, fair point PHV. I'm sending certain tasks from MS Project to a separate Outlook calendar called 'Project Calendar' and have been having problems with Outlook being left in the background, which is the reason for the question above. Apart from the above code, the actual interaction is via the 2 subroutines below, one which clears old appontments, the other which creates new ones. And for some reason it sometimes (can't find a set cause yet) leaves Outlook open in the background which seems to cause problems when I then open in foreground. apart from that the code seems to do exactly what I want.

You guys are usually great at spotting silly mistakes. Any ideas?

Thanks




Code:
Sub Create_An_Appointment(Subject As String, Start_Date As Date, Finish_Date As Date, Body As String)

    Dim olApp As Outlook.Application
    Dim objAppt As Outlook.AppointmentItem
    Dim olFolder As Outlook.MAPIFolder

    Set olApp = Outlook.Application

    Set olFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Parent.Folders("Project Calendar")
    'Create e-mail item
    Set objAppt = olFolder.Items.Add
    With objAppt
        .Subject = Subject
        .Start = Start_Date
        .End = Finish_Date
        .Location = Project_Title
        .Body = Body
        .Categories = Project_Title
        .ReminderSet = False
        .Save
    End With
    Set olApp = Nothing
End Sub

Sub Clear_Appointments()

    Dim olItems As Outlook.Items
    Dim olFolder As Outlook.MAPIFolder
    Dim N As Integer
    Dim NTotal As Integer
    Outlook_Update_Form.StatusBox.Visible = True
    Outlook_Update_Form.StatusBox = "Clearing "

    Set olApp = Outlook.Application

    Set olFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Parent.Folders("Project Calendar")
    Set olItems = olFolder.Items
    NTotal = olItems.Count
    For N = NTotal To 1 Step -1
        Outlook_Update_Form.StatusBox = "Clearing Calendar items: " & Format(N / NTotal, "0.0%") & " remaining"
        DoEvents
        If olItems(N).Location = Project_Title Then olItems(N).Delete
    Next N
    Outlook_Update_Form.StatusBox = "Calendar items cleared"
    Set olApp = Nothing
End Sub
 
I'm guessing that you mean

Set olFolder = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Parent.Folders("Project Calendar")

in stead of

Set olFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Parent.Folders("Project Calendar")

both places?

Such as this, might create implicit instantiation of extra instances of the automated application (quite a sentence, eh?), and might leave an extra instance in memory after the code has run.

Roy-Vidar
 
Hm, shouldn't you use either

Set olApp = New Outlook.Application

or use CreateObject/GetObject to instantiate?



Roy-Vidar
 
I don't do much in MS Project or Outlook VBA but within Sub Clear_Appointments()

Outlook_Update_Form....

looks like it could be an unqualified reference, it should be qualified within your olApp object.

And now looking at olApp I see;

olApp has not been Dimmed in Sub Clear_Appointments(); Are you using Option Explicit at the top of your code, I guess not; you should do that.

and...

Set olApp = Outlook.Application

should'nt that be one of the following;

Set olApp = New Outlook.Application or
Set olApp = CreateObject(Outlook.Application) or
Set olApp = GetObject(,OutLook.Application
 
Sorry my bad;

Set olApp = CreateObject("Outlook.Application") or
Set olApp = GetObject(,"OutLook.Application")
 
Hey Guys
So many replies! Lots of things to consider and I'll get back to you.
 
Well it turned out to be a mixture of things.

HughLerwill, dead right about the lack of variable declaration and unqualified references. So a star for you. Though oddly I needed to declare as an Object, not Application as I thought it should be.

Roy-Vidar - olApp.GetNamespace, clearly right - so star for you too. As a matter of interest 'Set olApp = New Outlook.Application' gave me problems.

And PH, pointing out that Outlook is a mono instance allowed me to remove the Outlook_is_Running() function all together as being redundant! So, not that he needs any more stars [thumbsup2], another for you.

Finally none of you pointed out that I hadn't set the objects to Nothing at the end. I found that was something else that caused Outlook to remain active. But I can't give myself a star, and anyway it would be bad form [blush]

So all in all a fairly typical Tek-tips reply which gave me what I needed rather than what I wanted!

Again, Many Thanks to you all.

Simon Rouse

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top