Script to automatically attach folder contents

Joined
Jan 18, 2007
Messages
2
Reaction score
0
Hi All.

I'm trying to attach the contents of a folder to send in outlook. The folder usually only contains 2 or 3 files, however the file names change on a daily basis and therefore I can't refer to them by name. Is there a way to refer to the entire contents of the folder?
 

Ian

Administrator
Joined
Feb 23, 2002
Messages
19,873
Reaction score
1,499
I'm not aware of how to do this myself (although I am sure it is possible). How about ziping the folder up, that only takes two clicks? Then it is only a single file to attach :)
 
Joined
Jan 18, 2007
Messages
2
Reaction score
0
Thanks Ian, but I should have been more specific. I am trying to attach files created in excel and using command buttons and VBA script am saving a series of packing lists, printing off copies and sending the numbered files as outlook attachments all at the click of a button. The recipient has some other program that opens the attachment and processes them automatically at his end. For whatever reason he is unable to do so if the are zipped. I have managed to write scripts to do all that apart from being able to attach the files. If the file was named the same each time that would be no problem using for eg:

.Attachments.Add ("f:\My Files\1234.xls"),

I thought of trying:

.Attachments.Add ("f:\My Files\*.*.xls")

but this doesn't work

Any ideas???
 
Joined
Oct 6, 2017
Messages
1
Reaction score
0
Sounds like, since there is more than one file you need to attach at a time that you probably need to loop in that folder to capture each and every xls file?

You can do this with a foreach loop or for loop.

Attaching them to outlook may be tricky.

You can do this an incorporate a for or foreach loop into it to create multiple attachments.

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
AddAttachment()
End Sub

Private Sub AddAttachment()
Dim mail As Outlook.MailItem = _
TryCast(Me.Application.CreateItem _
(Outlook.OlItemType.olMailItem), _
Outlook.MailItem)

mail.Subject = "An attachment for you!"

/********************* Modify *************************/
Dim attachment As OpenFileDialog = _
New OpenFileDialog()

attachment.Title = "Select a file to send"
attachment.ShowDialog()

If attachment.FileName.Length > 0 Then
mail.Attachments.Add( _
attachment.FileName, _
Outlook.OlAttachmentType.olByValue, _
1, _
attachment.FileName)

/**************** To New Code Added ***********************/

/* VB code loops through files and attaches.
Re: Email with multiple attachments

With a comma-separated list of full file names in the B cell, replace the line:
Code: (Note: You could use a variable for the file and even file path? That is what I do in SSIS)
*/
.attachments.Add filepath
//with:
//Code:
Dim files As Variant, file As Variant
//FilePath
files = Split(C:\_BISolutions\*.xls, ",")

For Each file In files
.attachments.Add file

Next

/********************************************************/
mail.Recipients.Add("Armando Pinto")
mail.Send()
End If
End Sub
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top