Cancel common dialogs

Hi Experts!

I don't quite get how the code works for commonDialogs

This is what I have so far, and it works like I want it to, except for if the user hits the cancel button.


  On Error GoTo errhandler
 
  CommonDialog1.Filter = ".txt"
  CommonDialog1.FileName = "MyFile.txt"
  CommonDialog1.ShowSave
 
   If Right(CommonDialog1.FileName, 4) <> ".txt" Then
        FileToSave = CommonDialog1.FileName & ".txt"
    Else
        FileToSave = CommonDialog1.FileName
    End If
   

I thought that when the user hit the cancel button, the error handler would run, but it doesn't.
The if statements are so if the user types in a filename without the extension, then it will automatically add the extension to it before it saves, just like other windows programs. Maybe there's a different way to do that??

The problem is if the user hits cancel, then it still runs the rest of the code, instead of going to the error handler like I thought it would.
I already tried stuff like:
If FileToSave = "" then exit sub
but the dialog filename keeps its value instead of being = to ""

What am I doing wrong?

Thanks!
istc

istcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
You have to set
CommonDialog1.CancelError = True
before the cancel button will raise an error condition.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
istcAuthor Commented:
That was fast! You must have some way to monitor new questions or else you just happened to look right after I posted.

It works great now. Thank you!

istc
0
Erick37Commented:
I just happened to reload my browser at the right time :-)

Glad to help.
0
Erick37Commented:
Actually, the .DefaultExt property will specify a default extention automatically if one is not supplied.


With CommonDialog1
..CancelError = True
..Filter = "Text files|*.txt"
..DefaultExt = "txt"
..ShowSave
End With
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.