Attach file and send email

Status
Not open for further replies.

jedrei

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
i have a bunch of files on a folder and i am looking for a code to attach 1 file and send email one at a time.

E. G. 30 files will send 30 emails with 1 attachment each and move the file to "SENT" folder

C:\ATTACHMENTS\ >>> files stored

C:\ATTACHMENTS\SENT\ >>> files will be moved here if it is already sent
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Description: Use the GetFolder function from the Microsoft Scripting Runtime FileSystemObject to get the Files list. Loop through that list and call for each file using Outlook's CreateItem function to create a new MailItem. Append the file to the MailItem´s Attachment.Add function. Then move the file.

I don't have code that moves the files, but i have this code that sends each file in an email -

Code:
Sub SendFilesinFolder()
   Dim sFName As String
 
   sFName = Dir("C:\Users\Diane\")
   Do While Len(sFName) > 0
     Call SendasAttachment(sFName)
     sFName = Dir
   Loop 
 
End Sub 
 
Function SendasAttachment(fName As String) 
 
Dim olApp As Outlook.Application 
 
Dim olMsg As Outlook.MailItem 
 
Dim olAtt As Outlook.Attachments 
 
Set olApp = Outlook.Application 
 
Set olMsg = olApp.CreateItem(0) ' email 
 
Set olAtt = olMsg.Attachments 
 
' attach file 
 
olAtt.Add ("C:\Users\Diane\" & fName) 
 
' send message 
 
With olMsg
 .Subject = "Here's that file you wanted"
 .To = "alias@domain.com"
 .HTMLBody = "Hi " & olMsg.To & "," & vbCrLf & "Attached is " & fName & " you requested."
 .Send 
 
End With 
 
End Function
 

suhasjose

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Thank yo uso much for this code! I had been using this for th epast 2 months which helped me to save a lot of time. Recently I came across a new scenario.. I need to attach all the files begining with same file name to a single email. For example; My folder contains 20 files. 5 of them have unique names (say 1 - 5) and they should go in five emails.Rest of the files have the same name and an extention (100,100#1,100#2, 200,200#1,200#2,200#3). Is there any way that I can attach all the files with which has same name before"#" into a single email?!

Thanks again for your help!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It's doable. The do while / loop is moved to the function... it won't loop through looking for all the 100's, then all the 200's - you'll need to run it for each set of files. If you routinely use the same set of filenames, you could use an array to loop through them and do it in one step.

Code:
Sub SendFilesbyEmail() 
 
Call SendFiles("C:\Users\diane\Test\") 
 
End Sub 
 
Function SendFiles(fldName As String) 
 
Dim fName As String 
 
Dim strName As String 
 
Dim sAttName As String 
 
Dim olApp As Outlook.Application 
 
Dim olMsg As Outlook.MailItem 
 
Dim olAtt As Outlook.Attachments 
 
Set olApp = Outlook.Application 
 
Set olMsg = olApp.CreateItem(0) ' email 
 
Set olAtt = olMsg.Attachments 
 
fName = Dir(fldName) 
 
strName = InputBox("First 3 characters in filename?")
Do While Len(fName) > 0
 If Left(fName, 3) = strName Then
   olAtt.Add fldName & fName
   sAttName = fName & "<br /> " & sAttName
  End If
 Debug.Print fName
  fName = Dir 
 
Loop 
 
' send message 
 
With olMsg
 .Subject = "Here's that file you wanted"
 .To = "alias@domain.com"
 .HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I have attached <br /> " & sAttName & "as you requested."
 .Display 
 
End With 
 
End Function
I added this code to Macro to send files by email
 

suhasjose

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Thanks a lot for replying!

The only problem that I would face here is - I do not know the file names in the folders! That can be any name. Not a specific set of names since multiple people use to save files there.. How can I have a solution for this? Should I get a list of the file names first?!

Thanks&Regards
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
With enough code, you could get the file names and sort by name and totally automate it. But it's more code than I have time for and I don't have any snippets handy that come close to doing that.

You'll could read the folder to get a list of file names then work with the list - you can use either vba or windows scripting host to bring it up.
 

suhasjose

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Okay! I will try to chase it using your hints! This is going to be a good learning for me!

Thanks a lot for you help and guidance:)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'm pretty sure there are some snippets of VBA or windows script/ vbscipt laying around the interwebs that will get a list of the files.
 

suhasjose

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I found some scripts in internet and combined everything together after making a few changes.. It works perfect now! The only problem that I face now is it skips file names if the name begins with a zero.

Thanks again Diane for your assistance..

---------------------------Dim strName As String

Sub SendFilesbyEmail()

Call ReadFiles("C:\Users\Test\")

End Sub

Function SendFilesArray(fldName As String)

Dim olApp As Outlook.Application

Dim olMsg As Outlook.MailItem

Dim olAtt As Outlook.Attachments

Dim fName As String

Dim sAttName As String

Dim arrName As Variant

Set olApp = Outlook.Application

'arrName = Array("2012", "2013", "2014")

' Go through the array and look for a match, then do something

'For i = LBound(arrName) To UBound(arrName)
'strName = arrName(i)

Set olMsg = olApp.CreateItem(0) ' email

Set olAtt = olMsg.Attachments

fName = Dir(fldName)


Do While Len(fName) > 0
If Left(fName, Len(strName) - 4) = Left(strName, Len(strName) - 4) Then
olAtt.Add fldName & fName
sAttName = fName & "<br /> " & sAttName
End If
'Debug.Print fName
fName = Dir

Loop

' send message

With olMsg
.Subject = "Here's that file you wanted" & fName
.To = "alias@domain.com"
.HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I have attached <br /> " & sAttName & "as you requested."
.Display

End With

sAttName = ""

'Next i

End Function

Function ReadFiles(MyFile As String)

Dim Counter As Long

ReDim DirectoryListArray(1000)

MyFile = Dir$(MyFile)

Do While MyFile <> "" And InStr(1, MyFile, "#") = 0
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1


Loop

ReDim Preserve DirectoryListArray(Counter - 1)

For Counter = 0 To UBound(DirectoryListArray)

strName = DirectoryListArray(Counter)
Debug.Print strName
Call SendFilesArray("C:\Users\Test\")

Next Counter

End Function
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
I Outlook 365 - import/attach PST file that used POP3 Using Outlook.com accounts in Outlook 0
A Order of Recent Items in Attach File List Using Outlook 5
M Same file "stuck" on top of file list in "Attach File" drop down Using Outlook 4
T Attach Current File to a new Email Using Outlook 0
P Macro to attach a file in a shared Outlook draft folder Outlook VBA and Custom Forms 2
T Outlook 2016 mp3 attach "this file cannot be preview because there is no previewer installed for it" Using Outlook 1
N Cannot paste image in the body of an email, it will only attach it as a file. Using Outlook 4
K Outlook 2007 can't find the file I want to attach. Using Windows 7 Home Using Outlook 2
W My document file cannot be accessed through the 'attach' icon. That file can only be attached by going to 'Documents' in master fi Using Outlook 2
R Unable to attach a large file to an email Using Outlook 1
L Attach file 'Look in' box only lists desktop and ftp Using Outlook 1
J Outlook 2010 shuts down and quickly restarts every time I try to attach a file, but otherwise works Using Outlook 2
R 404 File Not Found when trying to attach a file Exchange Server Administration 4
Y I need to attach a PDF file of resume and cover letter Using Outlook 1
N Outlook 2010, File Attachments, not being received by recipients, replaced with "winmail.dat" attach Using Outlook 35
T Outlook's button 'Attach File' does not work Outlook VBA and Custom Forms 7
M Files in "Attach File" popup menu no longer alphabetical Using Outlook 3
N How to attach a file with MS Outlook, from a program? Outlook VBA and Custom Forms 3
S Re: Cannot attach a file to an Outlook 2007 template after installingOffice 2007 SP2 Outlook VBA and Custom Forms 3
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
M Macro to attach last 2 items downloaded Outlook VBA and Custom Forms 5
Diane Poremsky Use a Macro to Attach Files to New Messages New Slipstick.com Articles 0
C Outlook Custom Contact Form Attach Files in new notes fields Outlook VBA and Custom Forms 3
B Outlook will not attach using Send To Feature Using Outlook 0
C Business Con Mgr - E-mail blast, to have a 'CC' as well as attach a document.. Exchange Server Administration 0
L Can't attach files in Outlook 2007 Using Outlook 2
M Can't attach filesd to email as no symbol Using Outlook 7
T Unable to attach message as an attachment in outlook Using Outlook 5
D Outlook 2010 error - you are attempting to attach too many files at once Using Outlook 11
J Also happened to me, My Outlook 2007 stopped allowing me to attach ANY files. Using Outlook 1
M Is there a Step-by-Step how-to Disable and Attach a Mailbox to a Different User for Exhange 2010? Exchange Server Administration 4
J Problem using drag and drop to attach an html email in Outlook 2010 Using Outlook 18
R My Outlook 2007 stopped allowing me to attach ANY files. Using Outlook 2
J How to attach several image files withour exceeding max message size ? Using Outlook 3
R Cannot attach documents to email Using Outlook 3
E Change Default Directory when attach or save files of emails Using Outlook 1
C Now the problem I see is that I get an error when installing the Exchange 2010 SP 1. I attach a scre Exchange Server Administration 10
A cant send a message when i attach a word doc__ message oa8f2vfv031886 Using Outlook 1
E Attach Item within Body of a Message in Outlook 2007 Using Outlook 1
C Unable to attach document Using Outlook 1
D 1-click change between "attach" and "in-line" for reply and forward? Using Outlook 1
P 1. Outlook 2007 won't send messages.__ Also:__2. I can't attach a 2nd email address to Outlook 2007. Using Outlook 1
J outlook 2010 will not attach contacts to address book Using Outlook 2
T Can't send email using "Attach to email" from Acrobat (and other programs) to Outlook Using Outlook 1
G Unable to attach some files Using Outlook 1
L not able to attach links to outlook express emails Using Outlook 1
D the 'attach item' function is not resizable. why? Using Outlook 4
R attach documents into tasks__ Using Outlook 2
P Send and save to a specific folder, send without saving, reply with or without attachement, delete attach files Using Outlook 3
K Slow to attach excel files Using Outlook 10
Similar threads


















































Top