I am a self taught vba user with not a high level. Most of my code is used by finding related code and editing it for my own use.
I have been using some vba code which uses SentOnBehalfOfName. One day it randomly stopped working. When the macro is run, it loads the email and then outlook closes down (no code to do that) outlook then re-opens and there is no draft email to use. If that step of code is removed, then the email loads fine and the user has to manually change the sent from to the required email. All users have permissions to send from the shared email. Another weird thing is that if outlook is closed before the macro is run, then the email is created and outlook doesn't crash. Anyone got any suggestions as to why this happens or what I can do to stop it happening?
Kind regards,
Tom
I have been using some vba code which uses SentOnBehalfOfName. One day it randomly stopped working. When the macro is run, it loads the email and then outlook closes down (no code to do that) outlook then re-opens and there is no draft email to use. If that step of code is removed, then the email loads fine and the user has to manually change the sent from to the required email. All users have permissions to send from the shared email. Another weird thing is that if outlook is closed before the macro is run, then the email is created and outlook doesn't crash. Anyone got any suggestions as to why this happens or what I can do to stop it happening?
Code:
Sub Create_Email()
On Error GoTo ErrHandler
' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Application.ScreenUpdating = False
Sheets("Email Information").Visible = True
Application.CalculateFull
' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.to = "Staff1@company.com; Staff2@company.com etc"
.sentOnbehalfofName = "sharedemail@company.com" <----- this is the step that is causing the issue
.cc = "Staff3@company.com; Staff4@company.com etc"
.Subject = "Results on Report"
.Body = "Put all information in here based on the results in report"
'Add the report as an attachment
.Attachments.Add ("File location")
.Display ' Display the message in Outlook.
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
'
Sheets("Email Information").Visible = False
Application.ScreenUpdating = True
End Sub
Kind regards,
Tom