'---------------------------------------------------------------------------
' NB: I have avoided abbreviating variable and procedure names to make
' things easier to grasp for beginners and non-native English speakers.
' Feel free to use Find and Replace to swap these names with something
' you prefer.
'---------------------------------------------------------------------------
'---------------------------------------------------------------------------
' One of the requirements for VBAX Knowledge Base submissions is that Option
' Explicit be used. As a rule of thumb, I always use Option Explicit to reduce
' mistakes, but some of the code in my testing sub procedure requires that it
' be turned off. See TestMyFolderExists below for more details.
'
Option Explicit
'---------------------------------------------------------------------------
'---------------------------------------------------------------------------
' Insert paths into the quotations below. These will be used to test the
' MyFolderExists function. The path may be a mapped drive path or a UNC Path.
' See the comments for the MyFolderExists function for more information.
'
' EG.: "D:\MyFolder\" (mapped drive path)
' EG.: "\\MachineName\VolumeName\MyFolder\" (UNC)
Private Const mcstrValidPathToFolder As String = "C:\temp"
Private Const mcstrInvalidPathToFolder As String = "C:\temp2"
'---------------------------------------------------------------------------
Private Sub TestMyFolderExists()
'---------------------------------------------------------------------------
' Desc : This sub procedure calls the MyFolderExists function using the
' module level constants defined above as the arguments,
' printing the results to the immediate window. You need to
' adjust the values of these strings for your own file system.
'
' Remarks : The purpose of this procedure is to demonstrate how the
' MyFolderExists function *might* be used. It is included
' here soley for that reason, and it is not required for the
' function to work.
'---------------------------------------------------------------------------
' NB: These constants can be replaced with any valid string enclosed by
' quotation marks
Debug.Print MyFolderExists(mcstrValidPathToFolder)
Debug.Print MyFolderExists(mcstrInvalidPathToFolder)
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
' Application Specific Examples Below:
'
' To use these examples, uncomment the block below and comment out Option
' Explicit. See comments at beginning of module for more details.
'
' NB: You must save your document, workbook, etc., if you want to use these
' application specific examples. If your project isn't saved, then by
' default the path will be an empty string.
' - - - - - - - - - - - - - - - - - - - - - - - - -
' Select Case Application.Name
' Case "Microsoft Access"
' Debug.Print MyFolderExists(CurrentProject.Path)
' Case "Microsoft Excel"
' Debug.Print MyFolderExists(ThisWorkbook.Path)
' Case "Microsoft PowerPoint"
' Debug.Print MyFolderExists(ActivePresentation.Path)
' Case "Microsoft Publisher", "Microsoft Word", "Microsoft Visio"
' Debug.Print MyFolderExists(ActiveDocument.Path)
' Case "Microsoft Project"
' Debug.Print MyFolderExists(ActiveProject.Path)
' Case Else
' 'Application not planned for; skip
' End Select
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
End Sub
Public Function MyFolderExists(Path As String) As Boolean
'---------------------------------------------------------------------------
' Desc : When given a path to a folder, this function returns TRUE if
' the folder exists and FALSE if it doesn't.
'
' Argument : The File System Object supports both mapped drive and UNC
' paths.
'
' The trailing slash does not matter. "C:\temp" and
' "C:\temp\" are equally valid: both return TRUE.
'
' Required : Microsoft Scripting Runtime (scrrun.dll)
'
' Remarks : The name of this function mirrors the FSO method used.
'---------------------------------------------------------------------------
Dim objFSO As Object
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set objFSO = CreateObject("Scripting.FileSystemObject")
Select Case objFSO.FolderExists(Path)
Case True
MyFolderExists = True
Case False
MyFolderExists = False
End Select
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set objFSO = Nothing
End Function
|