' 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 FixTrailingSeparator below for more details.
Option Explicit
' Insert paths into the quotation makrs below to test the function. The path
' may be a mapped drive path, UNC Path, or Macintosh path.
' eg.: "D:\MyFolder\" (mapped drive path)
' eg.: "\\MachineName\VolumeName\My Folder\" (UNC)
' eg.: "MyHardDriveName:My Documents:My Files" (Mac)
Private Const mcstrPathWithSeparator As String = "C:\temp\"
Private Const mcstrPathWithoutSeparator As String = "C:"
' Macintosh only: change the \ in the quotations below to :
Private Const mcstrPathSeparator As String = "\"
Private Sub TestMyTrailingSeparator()
'---------------------------------------------------------------------------
' Desc : This sub procedure calls the FixTrailingSeparator 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
' FixTrailingSeparator function *might* be used. It is included
' here soley for that reason, and it is not required for the
' function to work.
'---------------------------------------------------------------------------
' Note: These constants can be replaced with any valid string enclosed by
' quotation marks
Debug.Print FixTrailingSeparator(mcstrPathWithSeparator, mcstrPathSeparator)
Debug.Print FixTrailingSeparator(mcstrPathWithoutSeparator, mcstrPathSeparator)
' Application Specific Examples Below: to use these examples, uncomment
' the block below and comment out Option Explicit.
' Select Case Application.Name
' Case "Microsoft Access"
' Debug.Print FixTrailingSeparator(CurrentProject.Path, mcstrPathSeparator)
' Case "Microsoft Excel"
' Debug.Print FixTrailingSeparator(ThisWorkbook.Path, mcstrPathSeparator)
' Case "Microsoft PowerPoint"
' Debug.Print FixTrailingSeparator(ActivePresentation.Path, mcstrPathSeparator)
' Case "Microsoft Publisher", "Microsoft Word", "Microsoft Visio"
' Debug.Print FixTrailingSeparator(ActiveDocument.Path, mcstrPathSeparator)
' Case "Microsoft Project"
' Debug.Print FixTrailingSeparator(ActiveProject.Path, mcstrPathSeparator)
' Case Else
' 'Application not planned for; skip
' End Select
End Sub
Public Function FixTrailingSeparator(Path As String, _
Optional PathSeparator As String = "\") As String
'---------------------------------------------------------------------------
' Desc : Adds a trailing path separator to a folder path if it doesn't
' exist. Supports Windows or Macintosh.
'
' Arguments : (1) Path: Any path, Windows or Macintosh, UNC or mapped,
' valid or not.
' (2) PathSeparator: the default value is \, which is set
' for Windows. Macintosh users should use : for this
' argument.
'
' Remarks : This function looks at the last character in the string
' supplied in the Path argument. If it is the path separator,
' the function returns the string unchanged. If it is not the
' path separator, the function appends the path separator and
' returns the modified string.
'---------------------------------------------------------------------------
Select Case Right(Path, 1)
Case PathSeparator
FixTrailingSeparator = Path
Case Else
FixTrailingSeparator = Path & PathSeparator
End Select
End Function
|