O
Ouka
Hi all,
Is there an easy way to check for forbidden windows characters in a
script that takes the output of a textbox to use in a save as script?
Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
studyName = Trim(txbStudyName.Value)
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End Sub
--------------------
where txbStudyName is the name of a textbox in a user form and I want
to check studyName for any of the forbidden windows filename
characters.
Only way I've managed to do it is like so:
Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
Dim badChar1 As String
studyName = Trim(txbStudyName.Value)
badChar1 = InStr(1, studyName, "<")
If badChar1 <> "" Then
MsgBox "Study name is invalid."
Else
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End If
End Sub
--------------------
That just looks for a "<" character, and if found, creates an output.
Then if there is an output, throw a warning. I've had to make a
separate variable and elseif statement for each character, which makes
for rather cumbersome code. So I was wondering if there was a built-in
feature that would do this for me with just a line or two of code.
While I'm at is, is there a way to make VBA check the save file path
for any files with the same name before trying to save? i.e. if there
is already a file called "Study101.xls" in the destination path, have
VBA code throw the error instead of excel?
Thanks for yer help
--Ouka
Is there an easy way to check for forbidden windows characters in a
script that takes the output of a textbox to use in a save as script?
Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
studyName = Trim(txbStudyName.Value)
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End Sub
--------------------
where txbStudyName is the name of a textbox in a user form and I want
to check studyName for any of the forbidden windows filename
characters.
Only way I've managed to do it is like so:
Code:
--------------------
Private Sub CommandButton1_Click()
Dim studyName As String
Dim badChar1 As String
studyName = Trim(txbStudyName.Value)
badChar1 = InStr(1, studyName, "<")
If badChar1 <> "" Then
MsgBox "Study name is invalid."
Else
ActiveWorkbook.SaveAs Filename:=studyName & ".xls"
Unload Me
End If
End Sub
--------------------
That just looks for a "<" character, and if found, creates an output.
Then if there is an output, throw a warning. I've had to make a
separate variable and elseif statement for each character, which makes
for rather cumbersome code. So I was wondering if there was a built-in
feature that would do this for me with just a line or two of code.
While I'm at is, is there a way to make VBA check the save file path
for any files with the same name before trying to save? i.e. if there
is already a file called "Study101.xls" in the destination path, have
VBA code throw the error instead of excel?
Thanks for yer help
--Ouka