Attach any file in outlook through VBA and delete

earthworm

Well-known Member
Joined
May 19, 2009
Messages
759
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have found the below macro from net and made some changes .
This macro is adding specific file only . I need to add any type of file extension in outlook as an attachment from a specific folder path . Please help

please note that on later stage i will make the folder variable to pick any file from variable folder and once the email is sent, The files in that folder deletes . So that on next day i dont have to clear my folders up to run

VBA Code:
Sub Email_From_Excel_More_Options()

' Email

Dim emailApplication As Object
Dim emailItem As Object

Set emailApplication = CreateObject("Outlook.Application")
Set emailItem = emailApplication.CreateItem(0)

If Range("B1").Value > 0 Then

' To add more receipt use structure emailItem.to = "email1@test.com; email2@test.com; email3@test.com"
' emailItem.to = Range("C1").Value & "; " & Range("D1").Value & "; " & Range("E1").Value

emailItem.To = Range("C1").Value & "; " & Range("D1").Value & "; " & Range("E1").Value

emailItem.CC = "email1@test.com"

' emailItem.BCC = "email3@test.com"

emailItem.Subject = "Subject line for the email."

emailItem.Body = "The message for the email."

' Attach any file from your computer.

emailItem.Attachments.Add ("C:\Users\iiiii\OneDrive\Desktop\Test\1.txt")

' Send the Email
' emailItem.Send

' Display the Email so the user can change it as desired before sending it.
' Use this OR .Send, but not both together.
emailItem.Display

Set emailItem = Nothing
Set emailApplication = Nothing

Else
End If
 
So the variable should be at the end. Sorry missed that point. I assumed it referred to the iiii but that's the User

StrPath = ("C:\Users\iiiiii\OneDrive\Desktop\Test\" & Nme)

If you start the macro using the F8 key it steps through each line of the code and it is highlighted in in yellow.
If you hover over variables with the mouse a tooltip will show the value of the variable. Do it before the yellow line reaches the line of code with the variable of interest and after it passes it to see what value it holds.
This is very useful to confirm what you expect the variable value to be. Particularly helpful with Paths and filenames.
 
Last edited:
Upvote 0

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"

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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