excel VBA向一个电子邮件地址发送多封电子邮件

72qzrwbm  于 2023-02-05  发布在  其他
关注(0)|答案(2)|浏览(332)

我需要帮助我的VBA代码。我想让它当用户点击按钮发送电子邮件,它会根据单元格("A1:A4")自动发送电子邮件。
例如,如果今天是2023年2月2日,它将发送3封电子邮件,分别来自2月6日、2月13日和2月20日的电子邮件。
我做了VBA代码,但主要的问题是它只发送电子邮件的最后一个单元格("A4")。
对于("A2")和("A3"),将不发送电子邮件
[![在此输入图像说明][1]][1]

Sub Send_Deferred_Mail_From_Excel()
Dim OutlookApp As Object
Dim OutlookMail As Object

Dim xRg As Range
Set xRg = Range("A2:A4")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)

'Send Email Using Excel VBA Macro Code
With OutlookMail
.To = "email"
.CC = ""
.BCC = ""
.Subject = "HI"
.Body = "HELLO"

'Send email on specific day & time
.DeferredDeliveryTime = Range("A2") + Range("A3") + Range("A4")
.Display 'or just put .Send to directly send the mail instead of display

End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub
z31licg0

z31licg01#

请这样试一下.
在工作表(“Sheet1”)中列出:

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

宏将遍历“Sheet1”中的每一行,如果B列中有电子邮件地址,C:Z列中有文件名,则宏将使用此信息创建邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    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("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

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

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                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
End Sub
kmpatx3s

kmpatx3s2#

Sub Send_Deferred_Mail_From_Excel()
Dim OutlookApp As Object
Dim OutlookMail As Object

Dim xRg As Range
Set xRg = Range("A2:A4")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)

'Send Email Using Excel VBA Macro Code
With OutlookMail
.To = "email"
.CC = ""
.BCC = ""
.Subject = "HI"
.Body = "HELLO"

''Try in a loop instead.
for each cell in xRg
    'Send email on specific day & time
    .DeferredDeliveryTime = cell
    .Display 'or just put .Send to directly send the mail instead of display
next cell

End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub

相关问题