I am looking for code to check if a proposed save path (as string) is OK. However both the functions I have found so far - DO. NOT. WORK! 
I found this code on the net http://www.jpsoftwaretech.com/excel-...ate-filenames/
Function IsLegalFileName(ByVal str As String) As Boolean
If (str Like "<>:""/\|?*") Then
IsLegalFileName = True
End If
End Function
But it always returns False whenever I test it. The web page states: "I've modified the original code ever so slightly; the asterisks just inside the quotation marks in Ross' code are unnecessary, if the Visual Basic Help System is to be believed." I tried adding asterisks between the symbols but that only gives me a syntax error.
So I searched again and found this:
Function IsValidFileName(sFileName As String) As Boolean
Dim lstIllegal As Variant
Dim i As Long
Dim result As Boolean
lstIllegal = Array("/", "", ":", "*", "?", "< ", ">", "|", """")
result = True
For i = LBound(lstIllegal) To UBound(lstIllegal)
If InStr(1, sFileName, lstIllegal(i)) > 0 Then
result = False
Exit Function
End If
Next i
IsValidFileName = result
End Function
But this is no good because it doesn't check for all the invalid symbols (full list located here: http://msdn.microsoft.com/en-us/library/aa365247.aspx)
So I tried modifying the above code (see my attempt below) but I end up with Syntax error again!
Function IsValidFileName(sFileName As String) As Boolean
Dim lstIllegal As Variant
Dim i As Long
Dim result As Boolean
lstIllegal = Array("<", ">", ":", """, "/", "\", "|", "?", "*")
result = True
For i = LBound(lstIllegal) To UBound(lstIllegal)
If InStr(1, sFileName, lstIllegal(i)) > 0 Then
result = False
Exit Function
End If
Next i
IsValidFileName = result
End Function
Can anyone please provide a working function for what I am seeking and/or please let me know what is wrong with the above two functions?
Bookmarks