PDA

View Full Version : Save file, but remove illegal character



authorleon
03-10-2015, 05:19 PM
Hello,

I have this code:


baseFile.SaveAs _
FileName:=replaceText & ".prtl", _
FileFormat:=wdFormatText, _
AddToRecentFiles:=False


"replaceText " contact illegal characters. Is there a way to remove the illegal character please so I am able to save the file.

Thank you.

gmayor
03-10-2015, 11:49 PM
You could use the following function to clean the filename and make the modifications from the first block tio your code. Note that if replaceText includes the path you will have to add back the path to the filename before saving



Dim strFname As String
strFname = replaceText & ".prtl"
strFname = CleanFileName(strFname, "prt1")
'If necessary add back the path e.g.
'strfname = "C:\Path\" & strfname
baseFile.SaveAs _
Filename:=strFname, _
FileFormat:=wdFormatText, _
AddToRecentFiles:=False




Public Function CleanFileName(strFilename As String, strExtension As String) As String
'Graham Mayor 2015
Dim vfName As Variant
Dim lng_Name As Long
If InStr(1, strFilename, Chr(92)) > 0 Then
vfName = Split(strFilename, Chr(92))
CleanFileName = vfName(UBound(vfName))
vfName = Split(CleanFileName, Chr(46))
Else
vfName = Split(strFilename, Chr(46))
End If
CleanFileName = vfName(0)
If UBound(vfName) > 1 Then
For lng_Name = 1 To UBound(vfName) - 1
CleanFileName = CleanFileName & Chr(46) & vfName(lng_Name)
Next lng_Name
End If
vfName = Split(CleanFileName, Chr(11))
CleanFileName = vfName(0)
vfName = Split(CleanFileName, Chr(13))
CleanFileName = vfName(0) & Chr(46) & strExtension
CleanFileName = Replace(CleanFileName, Chr(34), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(42), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(47), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(58), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(60), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(62), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(63), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(124), Chr(95))
lbl_Exit:
Exit Function
End Function

authorleon
03-11-2015, 02:52 PM
You could use the following function to clean the filename and make the modifications from the first block tio your code. Note that if replaceText includes the path you will have to add back the path to the filename before saving



Dim strFname As String
strFname = replaceText & ".prtl"
strFname = CleanFileName(strFname, "prt1")
'If necessary add back the path e.g.
'strfname = "C:\Path\" & strfname
baseFile.SaveAs _
Filename:=strFname, _
FileFormat:=wdFormatText, _
AddToRecentFiles:=False




Public Function CleanFileName(strFilename As String, strExtension As String) As String
'Graham Mayor 2015
Dim vfName As Variant
Dim lng_Name As Long
If InStr(1, strFilename, Chr(92)) > 0 Then
vfName = Split(strFilename, Chr(92))
CleanFileName = vfName(UBound(vfName))
vfName = Split(CleanFileName, Chr(46))
Else
vfName = Split(strFilename, Chr(46))
End If
CleanFileName = vfName(0)
If UBound(vfName) > 1 Then
For lng_Name = 1 To UBound(vfName) - 1
CleanFileName = CleanFileName & Chr(46) & vfName(lng_Name)
Next lng_Name
End If
vfName = Split(CleanFileName, Chr(11))
CleanFileName = vfName(0)
vfName = Split(CleanFileName, Chr(13))
CleanFileName = vfName(0) & Chr(46) & strExtension
CleanFileName = Replace(CleanFileName, Chr(34), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(42), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(47), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(58), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(60), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(62), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(63), Chr(95))
CleanFileName = Replace(CleanFileName, Chr(124), Chr(95))
lbl_Exit:
Exit Function
End Function




This is GREAT. THANK YOU VERY MUCH.

I have just one issue, for some reason it ignores the first line. I am not sure why.

Thank you.

gmayor
03-11-2015, 11:00 PM
What 'first line' can you clarify?

authorleon
03-12-2015, 05:42 PM
What 'first line' can you clarify?

Hello gmajor and thank you.

When I create a list in MS word it, lets say:

Dog
Cat
Pig

It will only export cat and pig. But not dog.

Thank you

gmayor
03-12-2015, 10:52 PM
The code I posted has nothing to do with selecting lines in your document. It merely removes illegal filename characters from the string that was replaceText & ".prt1"

Your code that selects replaceText is not listed.