Prompt user for filename

  • Thread starter Thread starter taxidermist
  • Start date Start date
T

taxidermist

I am using the code example from http://support.microsoft.com/kb/161286/

It will open a file dialog for a user to select or type in a
filename. It works fine. However when the user types in the name of
a file that does not exist like "testfile" it will save it as
"testfile". What I need to be able to do is save it as "testfile.txt"
instead.

I thought it would be as simple as triming the filename and adding ( &
".txt" ) to the end of it but that seems not to be the case.

Can you please tell me what I need to do to make it save with the .txt
if the user does not type it in?

Thanks.
 
In this part of the code:
If lReturn = 0 Then
MsgBox "The User pressed the Cancel Button"
ElseMsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
End If

You can chec to see if the user put .txt on the end of the name and if it is
not there, add it. First dim a string variable like strFileName:


strFilename = OpenFile.lpstrFile

If Instr(strFileName, ".") = 0 Then
strFileName = strFileName & ".txt"
 
In this part of the code:
        If lReturn = 0 Then
            MsgBox "The User pressed the Cancel Button"
         Else>>> Code Goes Here >>>>

            MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
         End If

You can chec to see if the user put .txt on the end of the name and if it is
not there, add it.  First dim a string variable like strFileName:

    strFilename = OpenFile.lpstrFile

    If Instr(strFileName, ".") = 0 Then
        strFileName = strFileName & ".txt"

--
Dave Hargis, Microsoft Access MVP









- Show quoted text -

That didn't do it either. I did find the solution though. It was
right in front of my face the entire time.
I added this line of code:

OpenFile.lpstrDefExt = ".txt"

And that fixed it. =)
 
Back
Top