Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving Files - Naming Conventions

Status
Not open for further replies.

medieval

Technical User
Jun 18, 2003
31
GB
Firstly thanks for looking.

I am using the code ActiveWorkbook.SaveAs Filename:=Fanta
where Fanta is a variable string.

However in an instance where the string contains illegal characters like "/" or "\" or "&" for example is there a
script which will remove these illegal characters ?

For example if Fanta = "Oslo Company & Sons A/S", how
could you remove these offending characters ?

cheers,
Medieval.
 
Use this function

Code:
Private Function Remove_invalid_character(file_name As String) As String
' A file name can not contain any of the following characters /\:*?"<>|
'   This function replaces them by a dash
  file_name = Replace(file_name, "/", "-")
  file_name = Replace(file_name, "\", "-")
  file_name = Replace(file_name, ":", "-")
  file_name = Replace(file_name, "*", "-")
  file_name = Replace(file_name, "?", "-")
  file_name = Replace(file_name, Chr(34), "-")
  file_name = Replace(file_name, "<", "-")
  file_name = Replace(file_name, ">", "-")
  file_name = Replace(file_name, "|", "-")
  file_name = Replace(file_name, " ", "-")
  Remove_invalid_character = file_name
End Function
This last line is not necessary. It's just to avoid having spaces ...
Have a nice day
Rodie [2thumbsup]
 
Medieval,

Trouble is the path WILL contain \ What are you going to do about that?

You could use the REPLACE function.

Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 
Hi Rodie,

Thanks for the reply I will give that a go.

Hi Skip,

I intend to apply the script given by Rodie on the file name itself and not on the path which can be excluded from the process. Thanks for your tip too.

ta ever so.
Medieval.
 
When you stated
Code:
 ActiveWorkbook.SaveAs Filename:=Fanta
Fanta INCLUDES THE PATH.

Skip,

[red]Be advised:[/red] [glasses]
Alcohol and Calculus do not mix!
If you drink, don't derive! [tongue]
 
Hi Skip,

Fanta is a composite file name to which one of the consituted variable strings is denoted as "c:\crg".
This will be excluded from the process.

cheers,
Medieval
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top