PDA

View Full Version : VBA to Send Email Using NEW Outlook



Johno1974
10-11-2023, 04:59 AM
Hello,

I have now switched to the New Outlook, but the VBA code I use for sending emails to people appears not to work. I have been looking into this for weeks now and can't find a solution. Does anyone know how to fix this please?

Please see my VBA code below.

Thanks in advance. Kind regards,

Johno


Dim EmailAddr As String, OutApp As Object, OutMail As Object, strbody As String
Dim WSHnet As Object
Dim OL As Object, olAllUsers As Object, oExchUser As Object, oentry As Object, User As String
Set WSHnet = CreateObject("WScript.Network")
Set OL = CreateObject("outlook.application")
Set olAllUsers = OL.Session.AddressLists.Item("All Users").AddressEntries
User = OL.Session.CurrentUser.Name
Set oentry = olAllUsers.Item(User)
Set oExchUser = oentry.GetExchangeUser()
EmailAddr = ""
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hello. This is my email. " & vbNewLine & vbNewLine & _
"Regards," & vbNewLine & vbNewLine & _
"Me"
With OutMail
EmailAddr = 'myemailaddress'
.To = EmailAddr
.Subject = " Example Email"
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Load CloseCheck
CloseCheck.Show
End Sub

June7
10-11-2023, 10:17 AM
Please post code between CODE tags to retain indentation and readability.

When I run my old code it still opens message in the old Outlook. Change .Send to .Display and see what you get.

Logit
10-11-2023, 11:11 AM
This works here. ?????


Option Explicit

Sub emlsnd()
Dim EmailAddr As String, OutApp As Object, OutMail As Object, strbody As String
Dim WSHnet As Object
Dim OL As Object, olAllUsers As Object, oExchUser As Object, oentry As Object, User As String
Set WSHnet = CreateObject("WScript.Network")
Set OL = CreateObject("outlook.application")
'Set olAllUsers = OL.Session.AddressLists.Item("All Users").AddressEntries
'User = OL.Session.CurrentUser.Name
'Set oentry = olAllUsers.Item(User)
'Set oExchUser = oentry.GetExchangeUser()
'EmailAddr = ""
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hello. This is my email. " & vbNewLine & vbNewLine & _
"Regards," & vbNewLine & vbNewLine & _
"Me"
With OutMail
EmailAddr = "myemailaddress"
.To = EmailAddr
.Subject = " Example Email"
.Body = strbody
'.Send
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
'Load CloseCheck
'CloseCheck.Show
End Sub

Aussiebear
10-11-2023, 12:57 PM
welcome to VBAX Johno. When posting code to this forum please wrap your code with code tags. See the first line in my signature to see how this is done.