如何在Excel VBA中更改电子邮件的发送位置

dfty9e19  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(175)

我发现了Excel的VBA代码,它自动发送电子邮件,使用A列作为要发送的地址,B列是任何CC地址,C列是电子邮件的正文,D列是附件的文件路径。
我不需要列B包括抄送,但我需要从一个特定的电子邮件地址与同事共享的电子邮件发送。(不是我的默认个人,它目前这样做)
这段代码不是我自己写的,所以任何帮助都是很好的!
Here is how the Excel SS looks so far with currently 2 examples
下面是我目前拥有的VBA代码,到目前为止它工作得很好:

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("Sheet1")

Set OutApp = CreateObject("Outlook.Application")

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

    'Enter the path/file names in the D:Z column in each row
    Set rng = sh.Cells(cell.Row, 1).Range("D1:Z1")
    
    If cell.Value Like "?*@?*.?*" And _
    Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        
        With OutMail
            .to = sh.Cells(cell.Row, 1).Value
            .cc = sh.Cells(cell.Row, 2).Value
            .Subject = "Example Subject 1"
            .Body = sh.Cells(cell.Row, 3).Value
            
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell.Value) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            
            .Send 'Or use .Display/Send
        End With
        
        Set OutMail = Nothing
    End If
Next cell

Set OutApp = Nothing

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

我目前还没有尝试任何东西,因为我没有写代码本身,我的VBA知识是非常有限的!

ukdjmx9f

ukdjmx9f1#

在“with outmail”中,插入以下行:

.SentOnBehalfOfName = "email_address@domain.com"

因此,在您进行更改后,它应该看起来像这样,只需将“email_address@domain.com”替换为您想要使用的电子邮件地址。

With OutMail
         .SentOnBehalfOfName = "email_address@domain.com"
         .to = sh.Cells(cell.Row, 1).Value
         .cc = sh.Cells(cell.Row, 2).Value
         .Subject = "Example Subject 1"
         .Body = sh.Cells(cell.Row, 3).Value

相关问题