Link to home
Start Free TrialLog in
Avatar of jdc1944
jdc1944Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Replacing Reserved Characters in a Varible

I have a table in word where one of the cells in each row holds a name of a file to be created.

Currently my VB code goes through each row looking in this cell, gets the contents and stores this in a varible, opens a word template and then saves the template as whatever has been written in the cell.

No matter how many times i tell the users, they always forget they cannot use characters like ?\/<>: in this cell because they are not allowed in file names.  This obvisouly causes my code to error.

What i want it to do is check each character in the string for these reserved characters and if it finds one replace it with a hyphen so the code can carry on and save the template.

Im been messing around with InStr and Replace methods but im not getting very far.

Anyone able to help me?
Avatar of Bill Prew
Bill Prew

Given that you have a manageable set of characters to exclude, a simple REPLACE approach might work the best, for example:

strFile = Replace(strFile, "?", "-")
strFile = Replace(strFile, "\", "-")
strFile = Replace(strFile, "/", "-")
strFile = Replace(strFile, "<", "-")
strFile = Replace(strFile, ">", "-")
strFile = Replace(strFile, ":", "-")

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdc1944

ASKER

Thank you, both of them do just the job
Welcome.

~bp