Making an Email with attachment

saloelrn1988

New Member
Joined
Apr 14, 2020
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi,

really need your advise
1f62d[1].png

I'm trying to create a macro that creates an email with an attachment, however the code that I tried is giving me run time error please see below code:
Ps. the file that I tried to attached will vary depending on the month so I tried using some Cell that contains the folder and file name.


Sub Send_email()
'
' Send_email Macro
' Auto send email
'

' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Dim pageEditor As Object
Dim xinspect As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = Worksheets("Email data").Range("B2")
.CC = Worksheets("Email data").Range("B3")
.Subject = Worksheets("Email data").Range("B4")
.body = Worksheets("Email data").Range("B5")
source_file = "C:\Users\reomon\Desktop\Accrual - OUS\" & Worksheets("Email data").Range("B6") & "\" & Worksheets("Email data").Range("B7")
.Attachments.Add source_file
.display

End With

' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing

ErrHandler:
'
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this, to check if the file exists.

Write here literally what you have in cells B6 and B7

And put an image of windows explorer where i can see the folder name and file name.


VBA Code:
Sub Send_email()
  Dim objOutlook As Object, objEmail As Object
  Dim sPath As String, sFile As String
  '
  Set objOutlook = CreateObject("Outlook.Application")
  Set objEmail = objOutlook.CreateItem(0)
  sPath = "C:\Users\reomon\Desktop\Accrual - OUS\"
  '
  With Worksheets("Email data")
    sFile = sPath & .Range("B6").Value & "\" & .Range("B7").Value
    If Dir(sFile) = "" Then
      MsgBox "File not found"
      Exit Sub
    End If
    objEmail.To = .Range("B2").Value
    objEmail.CC = .Range("B3").Value
    objEmail.Subject = .Range("B4").Value
    objEmail.body = .Range("B5").Value
    objEmail.Attachments.Add sFile
    objEmail.display
  End With
  ' CLEAR.
  Set objEmail = Nothing: Set objOutlook = Nothing
End Sub
 
Upvote 0
Try this, to check if the file exists.

Write here literally what you have in cells B6 and B7

Try this, to check if the file exists.

Write here literally what you have in cells B6 and B7

And put an image of windows explorer where i can see the folder name and file name.


VBA Code:
Sub Send_email()
  Dim objOutlook As Object, objEmail As Object
  Dim sPath As String, sFile As String
  '
  Set objOutlook = CreateObject("Outlook.Application")
  Set objEmail = objOutlook.CreateItem(0)
  sPath = "C:\Users\reomon\Desktop\Accrual - OUS\"
  '
  With Worksheets("Email data")
    sFile = sPath & .Range("B6").Value & "\" & .Range("B7").Value
    If Dir(sFile) = "" Then
      MsgBox "File not found"
      Exit Sub
    End If
    objEmail.To = .Range("B2").Value
    objEmail.CC = .Range("B3").Value
    objEmail.Subject = .Range("B4").Value
    objEmail.body = .Range("B5").Value
    objEmail.Attachments.Add sFile
    objEmail.display
  End With
  ' CLEAR.
  Set objEmail = Nothing: Set objOutlook = Nothing
End Sub



VBA Code:
Sub Send_email()
  Dim objOutlook As Object, objEmail As Object
  Dim sPath As String, sFile As String
  '
  Set objOutlook = CreateObject("Outlook.Application")
  Set objEmail = objOutlook.CreateItem(0)
  sPath = "C:\Users\reomon\Desktop\Accrual - OUS\"
  '
  With Worksheets("Email data")
    sFile = sPath & .Range("B6").Value & "\" & .Range("B7").Value
    If Dir(sFile) = "" Then
      MsgBox "File not found"
      Exit Sub
    End If
    objEmail.To = .Range("B2").Value
    objEmail.CC = .Range("B3").Value
    objEmail.Subject = .Range("B4").Value
    objEmail.body = .Range("B5").Value
    objEmail.Attachments.Add sFile
    objEmail.display
  End With
  ' CLEAR.
  Set objEmail = Nothing: Set objOutlook = Nothing
End Sub
This works perfectly thanks!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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