创建新附件 Excel-VBA
Create new attachments Excel-VBA
有谁知道如何从 VBA 中删除附件对象?我正在尝试使用 VBA 在 Excel 中创建一个应用程序,这样它将根据在 excel 文件中找到的数据发送电子邮件。在我下面的代码中,发送了电子邮件,但是,最后一封电子邮件包含以前的所有附件。任何帮助将不胜感激。
Private Sub btnSendEmail_Click()
For Counter = 2 To 3
Dim Mail As New Message
Dim Config As New Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 465
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "test@gmail.com"
Config(cdoSendPassword) = "test"
Config.Fields.Update
Set curFirstName = Worksheets("Sheet1").Cells(Counter, 1)
Set curLastName = Worksheets("Sheet1").Cells(Counter, 2)
Set curEmail = Worksheets("Sheet1").Cells(Counter, 3)
Set curAttach = Worksheets("Sheet1").Cells(Counter, 4)
Mail.To = curEmail.Value
Mail.From = Config(cdoSendUserName)
Mail.Subject = "This is a test!"
Mail.HTMLBody = "<h1>" & curFirstName.Value & " " & curLastName.Value & "</h1>"
Mail.AddAttachment curAttach.Value
Mail.Send
Next Counter
MsgBox "Sent"
End Sub
下面这行代码应该可以工作:
Mail.Attachments.DeleteAll
有谁知道如何从 VBA 中删除附件对象?我正在尝试使用 VBA 在 Excel 中创建一个应用程序,这样它将根据在 excel 文件中找到的数据发送电子邮件。在我下面的代码中,发送了电子邮件,但是,最后一封电子邮件包含以前的所有附件。任何帮助将不胜感激。
Private Sub btnSendEmail_Click()
For Counter = 2 To 3
Dim Mail As New Message
Dim Config As New Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 465
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "test@gmail.com"
Config(cdoSendPassword) = "test"
Config.Fields.Update
Set curFirstName = Worksheets("Sheet1").Cells(Counter, 1)
Set curLastName = Worksheets("Sheet1").Cells(Counter, 2)
Set curEmail = Worksheets("Sheet1").Cells(Counter, 3)
Set curAttach = Worksheets("Sheet1").Cells(Counter, 4)
Mail.To = curEmail.Value
Mail.From = Config(cdoSendUserName)
Mail.Subject = "This is a test!"
Mail.HTMLBody = "<h1>" & curFirstName.Value & " " & curLastName.Value & "</h1>"
Mail.AddAttachment curAttach.Value
Mail.Send
Next Counter
MsgBox "Sent"
End Sub
下面这行代码应该可以工作:
Mail.Attachments.DeleteAll