简体   繁体   中英

Sending outlook emails from Excel

I have the below VBA that sends an email fine:

    With Sendrng

        ' Select the worksheet with the range you want to send
        .Parent.Select

        'Remember the ActiveCell on that worksheet
        Set rng = ActiveCell

        'Select the range you want to mail
        .Select

        ' Create the mail and send it
        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = ""

            With .Item
                .To = "123@321.com"
                .CC = ""
                .BCC = ""
                .Subject = "A new deli pre-order has been received."
                .Send
            End With

The part where I am now struggling is to set who the email has come from

I thought adding the below would work:

 .From = "111@222.com"

What happens when adding the above: No email is received at all

What am I missing?

You can try .SendUsingAccount to select the account which you are going to send the e-mail.

With .Item
    .SendUsingAccount = olAccount 'or some other account.
    .To = "123@321.com"
    .CC = ""
    .BCC = ""
    .Subject = "A new deli pre-order has been received."
    .Send
End With

您可能正在寻找.SenderEmailAddress.SentOnBehalfOfName

Try it this way.

Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .to = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "Hi there"
        .Attachments.Add ActiveWorkbook.FullName
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail1.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM