Hallo zusammen,
einer meiner Kunden hat ein Problem mit dem nachfolgenden Quellcode. Letztendlich soll der Codes nichts machen, als eine E-Mail über Outlook zu versenden.
Problem:
* Outlook ist geöffnet
* E-Mail wird über meine Applikation versandt
* Meine Applikation und Outlook wird geschlossen
* Outlook wird gestartet: Fehlermeldung, dass Outlook nicht gestartet kann, da das Profil beschädigt ist -> Profil muss neu erstellt werden.
Mich würden auch generelle Meinungen zum dem Code interessieren. Könnte man noch etwas optimieren? Ist der Code so OK?
Nebenbei: Outlook ist Version 2013
Vielen Dank für jedes Feedback!
Public Sub SendTest()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Const olMailItem = 0
Const olBCC = 3
Const olTo = 1
Try
'Prüfen, ob Outlook läuft
Dim OutlookRunning As Boolean = False
For Each Prozess As Process In Process.GetProcesses()
If Prozess.ProcessName.ToUpper.StartsWith("OUTLOOK") = True Then
OutlookRunning = True
End If
Next Prozess
'Outlook-Objekt erstellen
If OutlookRunning = False Then
objOutlook = CreateObject("Outlook.Application")
Else
objOutlook = GetObject(, "Outlook.Application")
End If
'Mail-Objekt erstellen
objOutlookMsg = objOutlook.CreateItem(olMailItem)
'Weitere notwendige Objekte erstellen
Dim objOutlookRecip As Object = Nothing
Dim objOutlookBCC As Object = Nothing
'Handle the E-Mail
With objOutlookMsg
objOutlookRecip = .Recipients.add("test1@test.de")
objOutlookRecip.type = olTo
objOutlookBCC = .recipients.add("test2@test.de")
objOutlookBCC.Type = olBCC
'Ich habe es auch bereits ohne explizite Angebe des Accounts probiert
For Each olAcc2 In objOutlook.Session.Accounts
If olAcc2.displayname = "TestAccount" Then
.SendUsingAccount = olAcc2
Exit For
End If
Next
.Subject = "This is as Test"
.Body = "Hello world!"
.send()
End With
objOutlookRecip = Nothing
objOutlookBCC = Nothing
objOutlookMsg = Nothing
objOutlook = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
Danke.
Gernot