[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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

0
istc
Asked:
istc
  • 3
1 Solution
 
Erick37Commented:
You have to set
CommonDialog1.CancelError = True
before the cancel button will raise an error condition.
0
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now