gubbfett
gubbfett

Reputation: 2177

Close outlook application

It is very possible that this question has been asked (many times) before but i can't find a solution.

I have a program that will send an email. The code looks something like this:

Dim oApp As Interop.Outlook.Application = Nothing
While oApp Is Nothing
    Try
        oApp = New Interop.Outlook.Application
    Catch ex As Exception
        oApp = Nothing
        Dim result As Integer = MessageBox.Show("Outlook is already running, close Outlook and click OK to continue sending this email or click Cancel to skip sending this email.", "Outlook", MessageBoxButtons.OKCancel)
        If result = DialogResult.Cancel Then
            Exit Sub
        End If
    End Try
End While

So far so good. The user is forced to close outlook to use the outlook application. When this is done, i do this:

Dim eMail As Interop.Outlook._MailItem
eMail = oApp.CreateItem(Interop.Outlook.OlItemType.olMailItem)

'To who?
eMail.To = "[email protected]"

'Sbuject
eMail.Subject = "Foo"

'Message
Dim message As String = "Bar"
eMail.HTMLBody = message

'Send
eMail.Send()

Works like a charm, but when this is done i have this little icon in the tray telling med that outlook is running, and if i try to open my outlook client i get a message that outlook is allready running. I have to click the icon in the tray to close before i can use the client. How do i close this application from my code when the mail is sent?

I have tried this with no sucess:

oApp.Quit()
oApp.Application.Quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
oApp = Nothing

Any help is appreciated.

Upvotes: 1

Views: 1342

Answers (0)

Related Questions