Remove special characters from file name (when saving)

totalnovice2

Registered User.
Local time
Today, 23:26
Joined
May 21, 2013
Messages
36
Hello.

I hope someone can help. I use the below code as part of a database which creates documents and then saves them using data from the form fields.

Every now and again one of the form fields contains a "special" character such as /,@,& and this prevents the file from saving.

Is there a simple addition to the below code to remove special characters from the file name if there is one present? It causes all sorts of issues as many people don't realise that there has been a problem until it is too late.

Fingers crossed someone can help :)

Code:
        objWord.ActiveDocument.SaveAs2 FileName:="C:\Users\Public\" & Forms![Front Page]![Site 2 Name] _
         & " " & Forms![Front Page]![Combo79] & " " & "O2" & ".doc"
 
         objWord.ActiveDocument.Close
 
Last edited:
Use the instr function to detect the special characters to prevent the save from happening.
When they are detected "prompt" a message saying these cant be used.

Alternatively, you can use the Replace function to replace any special chars by a default _ or some other character you choose.

Good luck
 
Thank you for the reply. Do you have an example of the coding which I could use please?

I'm not sure of the correct format to use.
 
use replace multiple times

dim filename as string
filename ="C:\Users\Public\" & Forms![Front Page]![Site 2 Name] _
& " " & Forms![Front Page]![Combo79] & " " & "O2" & ".doc"

replace(filename,"/","")
replace(filename,"?","")
replace(filename,"\","")

objWord.ActiveDocument.SaveAs2 FileName:= filename


if the character exists, all examples of it will be removed.

alternatively, you could remove any character that isn't an expected character, but this coding would be a bit more advanced.
 
What Dave said, or try typing Instr or Replace into the help and see how far you get.
Come back with any specific questions you have
 
Rather than use Replace() multiple times, how about this?

Code:
Public Function RemoveIllegalCharacters(ByVal strText As String) As String

    Const cstrIllegals As String = "\,/,:,*,?,"",<,>,|"
    
    Dim lngCounter As Long
    Dim astrChars() As String
    
    astrChars() = Split(cstrIllegals, ",")
    
    For lngCounter = LBound(astrChars()) To UBound(astrChars())
        strText = Replace(strText, astrChars(lngCounter), vbNullString)
    Next lngCounter
    
    RemoveIllegalCharacters = strText

End Function ' RemoveIllegalCharacters
 
Last edited:
use replace multiple times

dim filename as string
filename ="C:\Users\Public\" & Forms![Front Page]![Site 2 Name] _
& " " & Forms![Front Page]![Combo79] & " " & "O2" & ".doc"

replace(filename,"/","")
replace(filename,"?","")
replace(filename,"\","")

objWord.ActiveDocument.SaveAs2 FileName:= filename
One problem with this is that if you replace the backslashes, the file path will no longer be valid. Just needs to be the actual file name.
 
true - I was just thinking of slashes being an illegal special character he might want to take out. (or not thinking, as it turns out) :D
 

Users who are viewing this thread

Back
Top Bottom