Sending an Email from Excel when Outlook not available using VBA

倾然丶 夕夏残阳落幕 提交于 2021-02-10 14:33:39

问题


I want to send stakeholders an e-mail when a subordinate makes any updates in the Excel worksheet. I hope to use a Workbook_BeforeSave event where an e-mail is triggered from the subordinate's Outlook account.

The subordinate/user needs Outlook configured/installed in their system. If not mail wont be triggered.

Is there any way to overcome this, like sending the mail triggering request to a remote computer/server where Outlook is preconfigured and sending the mail from that computer/server to the stakeholder using a common or centralized Email id?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim OutApp As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Recipient
    Dim Recipients As Recipients

    Set OutApp = CreateObject("Outlook.Application")
    Set objOutlookMsg = OutApp.CreateItem(olMailItem)

    Set Recipients = objOutlookMsg.Recipients
    Set objOutlookRecip = Recipients.Add("receiver@domain.com")
    objOutlookRecip.Type = 1

    objOutlookMsg.SentOnBehalfOfName = "sender@domain.com"
    objOutlookMsg.Subject = "Testing this macro"
    objOutlookMsg.HTMLBody = "Testing this macro "

    For Each objOutlookRecip In objOutlookMsg.Recipients
        objOutlookRecip.Resolve
    Next

    objOutlookMsg.Display
    objOutlookMsg.Send

    Set OutApp = Nothing

End Sub

回答1:


Option Explicit

Private Sub CommandButton1_Click() On Error GoTo ErrHandler

' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
    .to = "webadmin@encodedna.com"
    .Subject = "This is a test message from Arun Banik"
    .Body = "Hi there"
    .Display        ' DISPLAY MESSAGE.
End With

' CLEAR.
Set objEmail = Nothing:    Set objOutlook = Nothing
    

ErrHandler: ' End Sub

All you need to do is change the .Display property with .Send property.

With objEmail
    .to = "arunbanik21@rediffmail.com"
    .Subject = "This is a test message from Arun"
    .Body = "Hi there"
    .Send       ' SEND THE MESSAGE.
End With

For more please check here, https://www.encodedna.com/excel/send-email-from-excel-using-vba-and-outlook.htm




回答2:


Follow the steps,

  1. We need to send emails from Outlook. Since Outlook is an outside object first thing we need to do is to set the object reference to “Microsoft Outlook 16.0 Object Library”.
  2. In VBA, Go to Tools > References.
  3. Now we will see the object reference library. In this window, we need to set the reference to “Microsoft Outlook 16.0 Object Library.”
  4. After setting the object reference, click on, Ok.
  5. Now we can access Outlook object in VBA coding
Sub SendEmail_Example1()
    Dim EmailApp As Outlook.Application 'To refer to outlook application
    Set EmailApp = New Outlook.Application 'To launch outlook application
    Dim EmailItem As Outlook.MailItem 'To refer new outlook email
    Set EmailItem = EmailApp.CreateItem(olMailItem) 'To launch new outlook 
    email
    EmailItem.To = "Hi@gmail.com"
    EmailItem.CC = "hello@gmail.com"
    EmailItem.Subject = "Test Email From Excel VBA"
EmailItem.HTMLBody = "Hi," & vbNewLine & vbNewLine & "This is my first email from Excel" & _
                    vbNewLine & vbNewLine & _
                    "Regards," & vbNewLine & _
                    "VBA Coder" 'VbNewLine is the VBA Constant to insert a new line
    EmailItem.Send
End Sub

For more, can you please give a try as mentioned in this article,

https://www.wallstreetmojo.com/vba-send-email-from-excel/



来源:https://stackoverflow.com/questions/66021845/sending-an-email-from-excel-when-outlook-not-available-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!