Gelöst: Absender Email From einstellen in Outlook vba
Wenn man versucht mit Vba Makro einen Absender in einer Email in Outlook einzustellen, dann führt das meistens zu einem Fehler.
Bisher habe ich noch keine Lösung im Internet gefunden.
Hier ist die Lösung, wie man einen Absender einstellen kann.
Man muss einen Loop durch die Outlook Account Adressen gehen und beim passenden Account den Account in die Email mit Set übernehmen.
Dim objAccount 'As Account For Each objAccount In app_Outlook.Session.Accounts If objAccount.SmtpAddress Like sEmail_From Then Set objEmail.SendUsingAccount = objAccount Exit For End If Next |
Folgend der richtige Code in vba
'< outlook > '*Ohne Verweis '*Outlook versionsunabhaengig, Late Binding Dim app_Outlook As Object Set app_Outlook = CreateObject("Outlook.Application") Dim objEmail As Object Set objEmail = app_Outlook.CreateItem(0)
' '*Mit Verweis '* bei Verwendung von Outlook mit Verweis Early Binding ' Dim app_Outlook As Outlook.Application ' Set app_Outlook = New Outlook.Application ' Dim objEmail As MailItem ' Set objEmail = app_Outlook.CreateItem(olMailItem)
'</ outlook >
'< Sender > Dim sEmail_From As String sEmail_From = ActiveWorkbook.Names("varEmail_From").RefersToRange.Value2
Dim sName_From As String sName_From = ActiveWorkbook.Names("varName_From").RefersToRange.Value2
'objEmail.SentOnBehalfOfName = sEmail_From 'objEmail.Session.Accounts(sEmail_From)
If Not sEmail_From Like "" Then Dim objAccount 'As Account For Each objAccount In app_Outlook.Session.Accounts If objAccount.SmtpAddress Like sEmail_From Then Set objEmail.SendUsingAccount = objAccount Exit For End If Next End If '</ Sender >
'--< Send Email >-- |