如何发送带有条件附件的电子邮件?
How to send emails with conditional attachments?
当您使用 VBA 中的用户表单发送电子邮件时,是否可以选择有条件地添加附件?
假设
- 如果选中复选框 1 和 2,将附加 IMG1
- 如果选中复选框 3,将附加 IMG2
- 如果选中复选框 1、2 和 3,将附加 IMG1 和 IMG2
Private Sub CommandButton1_Click()
Dim AppOutlook As Outlook.application
Dim Mailtje As Outlook.MailItem
Dim strbody As String
Set AppOutlook = CreateObject("Outlook.Application")
Set Mailtje = AppOutlook.CreateItem(olMailItem)
Mailtje.Display
Mailtje.To = TextBox1.Value
Mailtje.CC = TextBox2.Value
Mailtje.Subject = "Test" & Format(Date, "dd/mm/yy")
Mailtje.HTMLBody = strbody
.Attachments.Add = IMG1.jpg
End Sub
像这样:
If checkbox1.Value And checkbox2.Value Then
Mailtje.Attachments.Add "C:\Test\Pic1.jpg" 'use the full path
End If
If checkbox3.Value Then
Mailtje.Attachments.Add "C:\Test\Pic2.jpg"
End If
当您使用 VBA 中的用户表单发送电子邮件时,是否可以选择有条件地添加附件?
假设
- 如果选中复选框 1 和 2,将附加 IMG1
- 如果选中复选框 3,将附加 IMG2
- 如果选中复选框 1、2 和 3,将附加 IMG1 和 IMG2
Private Sub CommandButton1_Click()
Dim AppOutlook As Outlook.application
Dim Mailtje As Outlook.MailItem
Dim strbody As String
Set AppOutlook = CreateObject("Outlook.Application")
Set Mailtje = AppOutlook.CreateItem(olMailItem)
Mailtje.Display
Mailtje.To = TextBox1.Value
Mailtje.CC = TextBox2.Value
Mailtje.Subject = "Test" & Format(Date, "dd/mm/yy")
Mailtje.HTMLBody = strbody
.Attachments.Add = IMG1.jpg
End Sub
像这样:
If checkbox1.Value And checkbox2.Value Then
Mailtje.Attachments.Add "C:\Test\Pic1.jpg" 'use the full path
End If
If checkbox3.Value Then
Mailtje.Attachments.Add "C:\Test\Pic2.jpg"
End If