PDA

View Full Version : VBA Code to attach (multiple) file(s)



sheffield
01-26-2018, 09:03 AM
Hi
I try to attach (multiple) file(s) from the sheet (Files).
To select (multiple) file(s), I insert * in excel cell (C) to the desired path (Cell B). After the selection, I can run the macro from (Configurator) sheet. Right now, I already have a VBA macro that creates an email without attachment.

I need a separate VBA code, which should attach (multiple) selected file(s) to the email.

Can someone help me to solve my problem?

thx

tom

SamT
01-26-2018, 01:46 PM
Tom, you should show the code that macro that creates an email without attachment.

Click the # Icon and paste the code between the Code Formatting Tags

sheffield
01-26-2018, 11:54 PM
Hi Same

I’m using macros from Ron de Bruin: rondebruin.nl/win/s1/outlook/bmail2.htm
thx
tom

paulked
01-27-2018, 12:42 AM
That code is for selected cells, not files. As SamT said, please post your code using the Code tags.

gmayor
01-27-2018, 12:49 AM
You probably want something like the following - note that this uses code from Ron's web site to start Outlook - http://www.rondebruin.nl/win/s1/outlook/openclose.htm and assumes that the worksheet illustrated is the active sheet.


Sub SendWithAttachments()
Dim olApp As Object
Dim olMail As Object
Dim xlSheet As Worksheet
Dim fso As Object
Dim LastRow As Long, lngRow As Long

Set fso = CreateObject("Scripting.FileSystemObject")
Set xlSheet = ActiveSheet
With xlSheet
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Set olApp = OutlookApp()
Set olMail = olApp.createitem(0)
With olMail
.Subject = "Please find fies attached"
.display
For lngRow = 9 To LastRow
If InStr(1, xlSheet.Cells(lngRow, 3), "*") > 0 Then
If fso.FileExists(xlSheet.Cells(lngRow, 2)) = True Then
.attachments.Add xlSheet.Cells(lngRow, 2)
End If
End If
Next lngRow
End With
On Error GoTo 0
lbl_Exit:
Set fso = Nothing
Set olApp = Nothing
Set olMail = Nothing
Exit Sub
End Sub

sheffield
01-27-2018, 01:55 AM
Hi Graham
I have got an Error …. "Sub or Function not defined"

Set olApp = OutlookApp

And it doesn't matter whether or not Outlook is running .

t

georgiboy
01-27-2018, 03:47 AM
OutlookApp()

gmayor
01-27-2018, 05:19 AM
I guess you missed

note that this uses code from Ron's web site to start Outlook - http://www.rondebruin.nl/win/s1/outlook/openclose.htm
which code provides that function?