Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Validating a filename

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
red6000 - 06 Feb 2006 21:26 GMT
Hi,

I have a userform with a textbox.  In this box the user will enter a name
that the word document will ultimately be saved as.

However, I'd like to build a validation that ensures the value of TextBox1
is okay (ie doesn't contain : \ / etc) before the macro actually runs and
thus provides a prompt for the user to correct:

I'm just not sure:

a) what all the invalid characters are for word filenames
and
b) how to see if any of (a) are present in TextBox1.Value

Thanks.
red6000 - 06 Feb 2006 21:35 GMT
Okay, I just need to know (a)

What would also be good... can you centre the text in a msgbox as I have
multiple lines (as per code below)?

I've done (b) with:

Dim SearchString, SearchChar, MyPos

SearchString = TextBox1.Value
SearchChar = ":"
MyPos = InStr(1, SearchString, SearchChar, 1)
If MyPos > 0 Then
MsgBox ("Please enter a subject without the character" + Chr(13) + Chr(13) +
SearchChar)
End If
SearchChar = "\"
MyPos = InStr(1, SearchString, SearchChar, 1)
If MyPos > 0 Then
MsgBox ("Please enter a subject without the character" + Chr(13) + Chr(13) +
SearchChar)
End If
SearchChar = "/"
MyPos = InStr(1, SearchString, SearchChar, 1)
If MyPos > 0 Then
MsgBox ("Please enter a subject without the character" + Chr(13) + Chr(13) +
SearchChar)
End If

> Hi,
>
[quoted text clipped - 12 lines]
>
> Thanks.
Jezebel - 06 Feb 2006 23:02 GMT
There are easier ways to check for the presence of particular characters --

1. Using Instr()  >> if instr(pText, "\") > 0 then

2. Using Like  >> if pText Like "*[\?\*//]*" then

Easier still, for your current purposes, is to use the CommonDialog control,
which also gives all the additional on-the-fly Explorer functionality that
users expect (like create a new folder, change folder, delete a previous
file, etc).

Separately, rather than trying to assess whether the filename is valid, a
better approach is simply to use it and trap the error if any. (Which you
have to do anyway -- there are more potential filesave errors than bad
characters in the filename. On top of which, there are *many* possible bad
characters apart from the obvious ones -- control characters for a start,
plus presumably you don't want your users pasting oddball strings using
Swahili and Mongolian unicodes). And using "\" in the filename isn't
necessarily invalid: the user might want to specify a sub-folder (which you
can prevent using the CommonDialog).

Do
   pFileName = [get filename from user]

   'Give the user a chance to cancel the action
   if len(pFileName) = 0 then
       exit do
   end if

   'Suck it and see
   on error resume next
   Document.SaveAs FileName:=pFileName
   pError = Err.number
   on error goto 0

   'It worked, so exit
   if pError = 0 then
       exit do
   end if

   'Failed, so try again
   ... Error message ...

Loop

> Hi,
>
[quoted text clipped - 12 lines]
>
> Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.