For some reason when I create a Email and add filles it Duplicates all the files??

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Seems to loop though folder file 2 times?

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 cmbdataas As String
        Dim Answer As String
        Dim xFile As String
        Dim Count As Long

  
    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"
   
      Answer = MsgBox("Do you need model Drawings", vbQuestion + vbYesNo + vbDefaultButton2, "Need Models Yes/No")
   
    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"
    
            If Answer = vbYes Then
            For Each FSOFile In FSOFolder.Files
            If FSOFile.Name Like "*" & ".SLDPRT" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
            ElseIf FSOFile.Name Like "*" & ".SLDASM" Then
            Attached_File = FSOFile
            .Attachments.Add Attached_File
           
            End If
            Next FSOFile
            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
            End If
            Next FSOFile
            End If
End With

        Unload Me
       

       
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I can't see how it would add the same file twice. If you had files with the same name but different extension you could get both.
 
Upvote 0
Can you put it on a file sharing site and post a link?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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