VBA code for adding attachment in Outlook not working...

CarlStephens

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

Can anyone advise why the below code is not adding the attachment to the email in Outlook? Screenshot is below the code. 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("3. Email New Joiners")
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
On Error Resume Next

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("C11", 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
.Display

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

1601556791891.png
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try removing your quotes from the path and filenames in cells C7, C8, and C9.
 
Upvote 0
I have removed the quotes and still it does not attach.
 
Last edited:
Upvote 0
No, don't remove the path, only remove the quotes ("") from your path and filename for each of the cells...
 
Upvote 0
Try removing or commenting out On Error Resume Next from your code, and try running it again. Do you get any errors? If so, on which line, and what does the error message say?
 
Upvote 0
Remove the = signs from the Attachments.Add lines.
 
Upvote 0
Thank you, you were both correct...I had to remove the quotation marks and remove the = sign. You are legends, thank you again.
 
Upvote 0
Hi,
Just to add on to it, how to avoid error if i have attachment on only one cell and the rest of the two are empty.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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
Back
Top