简体   繁体   中英

How to add sender to a macro that sends emails in VBA excel

I've found a piece of code on the internet that sends out emails with attachments depending on an excel spreadsheet's data. The code works perfectly but what i would like to know is how can i change the mailbox from which the emails are coming from? Currently they go from the user who presses send's mailbox. But what i would like is for them to come from a specific mailbox that the user has access to, see code below:

Sub Send_Files()
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set sh = Sheets("Sending Tool")

Set OutApp = CreateObject("Outlook.Application")

For Each cell In sh.Columns("D").Cells.SpecialCells(xlCellTypeConstants)

    'Enter the File Path in E1, you can add more files and extend the range if needed
    Set rng = sh.Cells(cell.Row, 1).Range("E1:K1")

    If cell.Value Like "?*@?*.?*" And _
       Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)

        With OutMail
            .to = cell.Value
            .Subject = "Email Header"
            ' This is the body of the email, change the next line to change the email's contents. use "<br>" to create a line break in the email
            .HTMLBody = "Good Morning " & cell.Offset(0, -3).Value & "," & "<br>" & "<br>" & "Body" 
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell

            .Send  'Or use .Display
        End With

        Set OutMail = Nothing
    End If
Next cell

Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

MsgBox ("Emails Sent Successfully")

End Sub

For the sake of my example let's call the mailbox userhelp@example.co.uk, I would like for this to be the outgoing mailbox so that should a recipient wish to reply it would come back there rather than the user's personal email.

尝试

.ReplyRecipientNames = "userhelp@example.co.uk"

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