Home > Enterprise >  VBA to send out emails with (conditional) attachments
VBA to send out emails with (conditional) attachments

Time:01-12

Been working with VBA for some moments now. I was wondering, when you use a userform in VBA to send out an email. Is there an option to conditionally add an attachment?

Example userform

Let's say if checkbox 1 & 2 is checked, IMG1 shows in the attachment and if checkbox 3 is checked, IMG2 is added in the email. If checkbox 1,2 and 3 are checked both IMG`s will be attached in the email?

And if you would like to embed an image based on the same values of the checkboxes? Is there an efficient way to code this?

Thanks in advance. I`m here to learn more about the possibilities with VBA and to automate some daily processes.

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

CodePudding user response:

Something like this:

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
  •  Tags:  
  • Related