Skip to content

Instantly share code, notes, and snippets.

@toronya
Last active August 29, 2015 14:03
Show Gist options
  • Save toronya/501402f3b210a8ca3fbf to your computer and use it in GitHub Desktop.
Save toronya/501402f3b210a8ca3fbf to your computer and use it in GitHub Desktop.
Replace Illegal Windows filename characters
'# convert Illegal Windows filename characters
'# ex. Sheet<1> => Sheet_1_
'# sample1
Function ValidFileName(invalidFileName As Variant) As Variant
Dim invalidArray
invalidArray = Array("*", """", ":", ";", "\", "/", "<", ">", "[", "]", "|", ",", "?", ".")
Dim c As Variant
For Each c In invalidArray
invalidFileName = Replace(invalidFileName, c, "_")
Next
ValidFileName = invalidFileName
End Function
'# sample2
Function ValidFileName(invalidFileName As Variant) As Variant
Dim invalidCharArray
invalidCharArray = Array("*", """", ":", ";", "\", "/", "<", ">", "[", "]", "|", ",", "?", ".")
'# system reserved
Dim invalidNameArray
invalidNameArray = Array("CON", "PRN", "AUX", "CLOCK$", "NUL", _
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", _
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9")
Dim c As Variant
For Each c In invalidCharArray
invalidFileName = Replace(invalidFileName, c, "_")
Next
For Each c In invalidNameArray
If invalidFileName = c Then
' strconv "vbWide" option : Japanese, Chinese, Korean
'invalidFileName = StrConv(invalidFileName, vbWide) '# ex. "CON" => "CON"
invalidFileName = invalidFileName & "_" '# ex. "CON" => "CON_"
End If
Next
ValidFileName = invalidFileName
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment