Closing a parent process nicely...is it that hard?

I'm trying to close Outlook (exchange 03) nicely, meaning that if there's an unsent message open, it will ask if you want to save, etc...

in vbscript it was easy as:

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'OUTLOOK.EXE'")
If colProcesses.Count > 0 Then
intAnswer = _
msgbox ("Outlook is open. For things to be backed up properly, Outlook must be closed" & vbcr & "Click OK to close Outlook now, or cancel to quit the backup.", 17,"Outlook is running...")
if intAnswer = vbok then
'quit outlook process
set olapp = CreateObject("Outlook.Application")
wshell.appactivate "Outlook"
olapp.quit
else
quit
end if
end if

However, in VB.net i'm only able to close the most recently open window in Outlook - rather than post my code which uses the GetProcessByName method, and confuse anyone, would someone be so kind as to how they would do it? I basically need something that is equivalent to clicking the "X" on the main Outlook window. I'll put it inside a loop as well to make sure that Outlook is closed before proceeding.

Thanks!

[1419 byte] By [sounddoc] at [2007-12-28]
# 1

I think we still support GetObject and CreateObject - you could keep using the code you have (slightly updated to declare variables with dim and as object.

After all, you're using Outlook's com api, which will probably work better than straight out killing outlook's process.

AlexMoura at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thanks Alex!

unfortunately the vbs doesn't work either. I there's no .quit or .close method available for the olapp object. All variables were declared and the program built and ran without errors until it tried to close and errored saying it couldn't find process {0} on the line: VisualBasic.Interaction.AppActivate("Outlook") (originally 'wshell.appactivate "Outlook"' in the vbs)

I'm finding it hard to believe that this should be so complicated in vb.net. When i try to attach to the process It kills the last activated window in Outlook - e.g. an unsent e-mail, or opened appointment, and for the life of me i can't seem to bind to the parent process and exit that nicely.

For help using the Outlook COM API is there another forum I should be posting in? I would imagine that this should be something as basic as create an outlook object (which would inherit all it's child windows as well), close the object (also applying to all it's child windows).

Thanks again - sorry for being so green

sounddoc at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
OK - some progress has been made. By using the simple method (I knew there should be something simple here):

Dim olapp As Outlook.Application = GetObject(, "Outlook.Application")
olapp.Quit()

I'm able to close Outlook nicely - all the windows, prompting to save etc...

The problem still remains though, that once all the windows are closed, an OUTLOOK.EXE process is still remaining. I seem to remember this happening if you have Word as your editor set, but this is not the case here. I can kill it using the process class, but i'd have to have that timed to when the application object is closed - i.e. the user no longer sees any open outlook windows. Am I missing something very obvious here?

sounddoc at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

sounddoc wrote:
rather than post my code which uses the GetProcessByName method,

If you are getting the process using the process class then...use the kill method to close "Kill" the process..

pseudo:

Dim MyProcesses() as process = Process.GetProcessByName(TheName)

For each p as Process in MyProcesses

p.kill

next

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
sounddoc wrote:

an OUTLOOK.EXE process is still remaining.

After the quit release the comm object...

system.Runtime.InteropServices.Marshal.ReleaseComObject(olapp)

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Beautiful! This is exactly what i was looking for!

in getting the object; GetObject(, "outlook.application) i was bound to it seemingly keeping the outlook process open, but needed to close it, something like "CloseObject(etc..)".

Thanks very much, the program is working like a charm now.
sounddoc at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...