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
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..
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
If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then
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
.Send()
Else
.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