VBA Code Error When Adding Attachments

CarlStephens

Board Regular
Joined
Sep 25, 2020
Messages
97
Office Version
  1. 2016
Platform
  1. Windows
Hello,

The below code errors when there is no file path address for the attachment and does not create the email. The below example, shows theres only 4/6 attachments and the last two cells (C11 & C12) are blank. What do I need to add to the code for the code to ignore these cells if they are blank and create the email? Thank you.

VBA Code:
Sub Send_Single_Email()

Dim xOutApp As Object
Dim xOutMail As Object
    Dim xMailBody As String
Dim ws As Worksheet
Set ws = Sheets("1")
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

With xOutMail
.To = ws.Range("C2").Value
.CC = ws.Range("C3").Value
.BCC = ws.Range("C4").Value
.Subject = ws.Range("C5").Value
.body = Join(Application.Transpose(ws.Range("C14", ws.Cells(ws.Rows.Count, "C").End(xlUp)).Value), vbCrLf)
.Attachments.Add ws.Range("C7").Value
.Attachments.Add ws.Range("C8").Value
.Attachments.Add ws.Range("C9").Value
.Attachments.Add ws.Range("C10").Value
.Attachments.Add ws.Range("C11").Value
.Attachments.Add ws.Range("C12").Value
On Error Resume Next
.Display

End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub

1634049897481.png
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,767
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Move the On Error Resume Next above the first Attachments.Add line, then put On Error Goto 0 after the last one.
 
Solution

CarlStephens

Board Regular
Joined
Sep 25, 2020
Messages
97
Office Version
  1. 2016
Platform
  1. Windows
Thanks Rory. I am now getting a display error. Do you know what is causing this?
1634053460145.png
 

Forum statistics

Threads
1,147,747
Messages
5,742,964
Members
423,769
Latest member
LongToast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top