Struggling with Email Attachments Code

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
With this code the last part of Send Email FSOFile says "FSOFile" rather than the path and name of file??

VBA Code:
For Each FSOFile In FSOFolder.Files
        If (FSOFile.Name Like "*" & ".pdf" Or FSOFile.Name Like "*" & ".STEP" Or FSOFile.Name Like "*" & ".DXF") Then
            Call Send_Email("Me.Email_List.Value", "", "", "Me.Enter_Number.Value" & " " & Format(Date, "dd/mmmm/yyyy"), _
            "Process Frost Drawings", "FSOFile")
        End If
    Next FSOFile

Main Code below

VBA Code:
Sub Send_Email(EmailTo As String, EmailCC As String, EmailBCC As String, EmailSubject As String, EmailBody As String, EmailAttachment As String)
    Dim EmailApp As Object
    Dim EmailItem As Object
    Dim Source As String

    Set EmailApp = CreateObject("Outlook.Application")
    Set EmailItem = EmailApp.CreateItem(0)
    
    With EmailItem
        .to = EmailTo
        .CC = EmailCC
        .BCC = EmailBCC
        .Subject = EmailSubject
        .Body = EmailBody
        
        If EmailAttachment <> "" Then
            .Source = EmailAttachment
            EmailItem.Attachments.Add Source
        End If
        
        .Display
        'EmailItem.Send
    End With
End Sub
Private Sub Email_Drawings_Click()

    Dim EmailApp As Object
    Dim EmailItem As Object
    Dim Source As String
    Dim FSOLibary As FileSystemObject
    Dim FSOFolder As Object
    Dim FSOFile As Object
    Dim strFolderCriteria As String, FolderName As String, strPath As String, strEmailTo As String
    Dim FilesToSend As String


    Set EmailApp = CreateObject("Outlook.Application")
    Set EmailItem = EmailApp.CreateItem(0)
    strFolderCriteria = (Me.Enter_Number.Value)
    strPath = "\\DF-AZ-FILE01\Company\R&D\Drawing Nos\Frost Grates"
    FolderName = strPath & "\" & strFolderCriteria & "\"
    Set FSOLibary = New Scripting.FileSystemObject
    Set FSOFolder = FSOLibary.GetFolder(FolderName)
    
    For Each FSOFile In FSOFolder.Files
        If (FSOFile.Name Like "*" & ".pdf" Or FSOFile.Name Like "*" & ".STEP" Or FSOFile.Name Like "*" & ".DXF") Then
            Call Send_Email("Me.Email_List.Value", "", "", "Me.Enter_Number.Value" & " " & Format(Date, "dd/mmmm/yyyy"), _
            "Process Frost Drawings", "FSOFile")
        End If
    Next FSOFile




End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If you want the path and name use:

VBA Code:
Call Send_Email("Me.Email_List.Value", "", "", "Me.Enter_Number.Value" & " " & Format(Date, "dd/mmmm/yyyy"), _
            "Process Frost Drawings", FSOFile.Path & application.pathseparator & fsofile.name)

Not sure what your .Source is supposed to be - a mailitem doesn't have a source property.
 
Upvote 0
I need to find all PDFs in a folder then send them on Email. With Rory`s code it says the pdf drawing twice.
 
Upvote 0
and probably change

If EmailAttachment <> "" Then
.Source = EmailAttachment
EmailItem.Attachments.Add Source
End If

to

If EmailAttachment <> "" Then
EmailItem.Attachments.Add EmailAttachment
End If
 
Upvote 0
This all works thanks, except i can`t seem to get the Email Address to work. As in Email list on the form Value?
 
Upvote 0
try removing the quotes from the variables
Me.Email_List.Value

in the routine send _email

add some debugging to show what is being passed to the routine

add
stop
debug.print EmailTo
and when code stops use F8 to step through the code


which will print into the immediate window the value of EmailTo
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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