Trying to make the code so if it can`t find a folder then msgbox comes up.

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
For some reason the msgbox does not come up.
See full code beow
VBA Code:
Private Sub Email_List_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, strEmailList As String
    Dim xMailbody As String

    On Error Resume Next
    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)
    If FSOFolder <> vbNullString = "" Then
        MsgBox "There is no Folder By this Name", "Please check your Part Code is correct"
        Exit Sub
    End If
    
    Select Case Time
        Case Is < TimeValue("12:00:00")
            xMailbody = "Good Morning"
        Case Is < TimeValue("17:00:00")
            xMailbody = "Good Afternoon"
    End Select
    
    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, "", "", "Dr.No." & " " & Me.Enter_Number.Value & " " & "Date Sent" & " " & Format(Date, "dd/mm/yyyy"), _
                  xMailbody & "," & vbNewLine & vbNewLine & "Process Frost Drawings." & vbNewLine & vbNewLine & "Kind Regards,", FSOFolder.Path & Application.PathSeparator & FSOFile.Name)
        Else: Exit Sub
        End If
    Next FSOFile
    
    
    
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I expect that the condition you have set is not true. Did you
a) step through the procedure and validate your variable values?
b) try a Debug.Print (or msgbox) fsofolder? That should point to the error of you code methinks (but not 100% sure as I'm rusty on this). You've declared fsofolder as an object yet you are trying to concatenate a zero length string to it, and then check it's string value. That makes me think you turned off error checking somewhere, assuming that doing so would negate an error message of that type. Maybe change the variable type (and the test for Is Nothing (or Empty) or invoke and compare fsofolder.name?
 
Upvote 0
Solution
Yes thanks I`ve managed to get it to work earlier but thanks anyway. What you said is more or less what i did so i will tick that box.
 
Upvote 0
Glad you got it working, and thanks for the recognition.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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