Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i am developing a vsto add-in in excel, which includes a batch mail sending tool. in some place, i am trying to catch if outlook is running with the following code

VB
If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then
    outApp = DirectCast(Marshal.GetActiveObject("Outlook.Application"), outlook.Application)
Else
    outApp = New outlook.Application
End If


it works fine while outlook is running in the foreground.

but when outlook is closed, in the first run, it still works but leaves the outlook running in background. and if i attempt to run the code second time, it doesnt work and give error at the above part of the code.

the whole code is below..
VB
Sub batchmailgonder()
        Dim outApp As outlook.Application
        Dim outMail As outlook.MailItem

        Dim fm As New frmBatchMail
bastan:
        fm.ShowDialog()

        If fm.devammi = 0 Then Exit Sub 'when clicked Cancel in the form

        If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then
            'here is the part where i get the error in the second part
            outApp = DirectCast(Marshal.GetActiveObject("Outlook.Application"), outlook.Application)
        Else
            outApp = New outlook.Application
        End If

        app.Range("a2").Select()
        Do While CStr(app.ActiveCell.Value) <> ""
            outMail = outApp.CreateItem(outlook.OlItemType.olMailItem)
            With outMail
                .HTMLBody = sRTF_To_HTML(fm.govde)
                .Subject = fm.konu
                .To = app.ActiveCell.Value
                If fm.devammi = 1 Then 'which means Go
                    .Send()
                Else 'which means preview
                    .Display()
                    outMail = Nothing
                    outApp = Nothing
                    fm.devammi = 0 
                    GoTo bastan
                End If
            End With
            app.ActiveCell.Offset(1, 0).Select()
            outMail = Nothing
        Loop

        outApp = Nothing
        MsgBox("Mail gönderimi tamamlandı")

    End Sub
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900