For some reason the For Each statement seems not work?

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
This part
VBA Code:
Next FSOFile
just says Next without for ???



VBA Code:
Private Sub Email_Files_Click()

        Dim objol     As Object
        Dim objmail   As Object
        Dim objFolder As Object
        Dim FSOLibary As FileSystemObject, FSOFolder As Object, FSOFile As Object
        Dim FolderName As String
        Dim SourcePath As String
        Dim SubPath As String
        Dim PdfFolder As Folder
        Dim PdfFile As String
        Dim MyPath As String
        Dim PDFFName As String
        Dim xMailbody As String
        Dim Morning_Afternoon As String
        Dim cmbdata
        Dim Answer As String

   
    Set objol = CreateObject("Outlook.Application")
    Set objmail = objol.CreateItem(0)
    
    cmbdata = Split(Me.OpenDrawing.Value, "-")
    cmbdata(0) = Replace(cmbdata(0), "-", "")
    
    SourcePath = "S:\R&D\Drawing Nos"
    
    If Val(cmbdata(0)) >= 10001 And Val(cmbdata(0)) <= 10050 Then
        SubPath = "10001-10050"
    ElseIf Val(cmbdata(0)) >= 10051 And Val(cmbdata(0)) <= 10100 Then
        SubPath = "10051-10100"
    ElseIf Val(cmbdata(0)) >= 10101 And Val(cmbdata(0)) <= 10150 Then
        SubPath = "10101-10150"
    ElseIf Val(cmbdata(0)) >= 10151 And Val(cmbdata(0)) <= 10200 Then
        SubPath = "10151-10200"
    End If

    FolderName = SourcePath & "\" & SubPath & "\" & Int(cmbdata(0))
    Set FSOLibary = New Scripting.FileSystemObject
    Set FSOFolder = FSOLibary.GetFolder(FolderName)
    Set FSOFile = FSOFolder.Files


    If Time < TimeValue("12:00:00") Then
    xMailbody = "Good Morning"
    ElseIf Time > TimeValue("12:00:00") And Time < TimeValue("17:00:00") Then
    xMailBoby = "Good Afternoon"
    End If
    
     
    
        With objmail
        .To = Email_Supplier.Value
        .cc = ""
        .BCC = ""
        .Subject = "All Files to Send" & " " & Format(Date, "dd/mmmm/yyyy")
        .Display
        .HTMLBody = xMailBoby & "," & _
        "<p> Please see Attached files to Process <P>" & _
        "Please see Attached files to Quote for<P>" & _
        "Mt"
        
        
            Answer = MsgBox("Do you need model Drawings", vbQuestion + vbYesNo + vbDefaultButton2, "Need Models Yes/No")
            

            If Answer = vbYes Then
            For Each FSOFile In FSOFolder.Files
            If FSOFile.Name Like "*" & ".SLDPRT" Then
            Attached_File = FSOFile
            ElseIf FSOFile.Name Like "*" & ".DXF" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
            ElseIf FSOFile.Name Like "*" & ".pdf" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
            Next FSOFile
             End If
             End If
            
            
            If Answer = vbNo Then
            For Each FSOFile In FSOFolder.Files
            If FSOFile.Name Like "*" & ".DXF" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
            ElseIf FSOFile.Name Like "*" & ".pdf" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
            Next FSOFile
            End If


        Unload Me
        

        
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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