Monday, July 6, 2009

Calling the Save As / Open / Common Dialog

NOTE: I'm experiencing an error when I distribute ArcMap projects using this method. I am fairly sure this is because I have Visual Basic (Studio) 6 installed on my main development workstation, and I'm attempting to call a control from Visual Basic Studio (VB6). Calling that control without VB6 installed separately will cause the application to fail when distributed to other machines.

I am currently looking for a VBA solution to this, but the code will indeed work well in VB6.
- Updated 10/29/2009




VB has been wonderful to me so far, however I'm trying to do some exporting and needed to figure out how to navigate around the hard drive for a directory to which I can save some files. The Common Dialog is the place to look to launch the specialized dialog boxes that let a user navigate around the hard/network drives on a computer.

The CommonDialog (one word, in the Visual Basic Editor toolbox) needs to be added to the VB Toolbox, then that control needs simply to be added to your form to be called by various procedures. Here's how it works:
  • Open a new Visual Basic project
  • Insert a new UserForm, then add the following controls
    (If it's not already open, activate the VB toolbox: View :: Toolbox)


Component/Property
Command Button
Command Button
Text Box
Text Box
Name
cmdOpen
cmdSaveAs
txtOpen
txtSaveAs


  • If it's not already open, activate the VB toolbox
  • Right click in an empty gray area on the toolbox and choose Additional Controls
  • Scroll down and put an X next to "Microsoft Common Dialog Control, version x" and click OK
  • Add a CommonDialog control to the form (named CommonDialog1 by default)
  • Enter the following code for the command buttons to call the respective dialog boxes:
Private Sub cmdOpen_Click()
'Show the Open dialog
CommonDialog1.ShowOpen

'Show the selected file to open
txtOpen.Text = CommonDialog1.FileName
End Sub
_____________________________________

Private Sub cmdSaveAs_Click()
'Show the Save As dialog
CommonDialog1.ShowSave

'Show the Save As filename
txtSaveAs.Text = CommonDialog1.FileName
End Sub


Run the form, and test it by launching the Open/Save As dialogs from the respective command buttons. Selecting a file will print the path in the adjacent text box.

From here one can call CommonDialog1.FileName in the place of a static/predefined path when opening or saving files from a form.

Other Show- methods include ShowColor (see MS Paint), ShowFont, ShowHelp, and ShowPrinter.

Keywords:
ArcObjects, VBA, VB6
common dialog, CommonDialog
OpenFile
SaveFile
Save As dialog
Open dialog

1 comment:

Anonymous said...

Regarding your comment "...NOTE: I'm experiencing an error when I distribute ArcMap projects using this method. I am fairly sure this is because I have Visual Basic (Studio) 6 installed on my main development workstation, and I'm attempting to call a control from Visual Basic Studio (VB6). Calling that control without VB6 installed separately will cause the application to fail when distributed to other machines....", I'm having the identical problem. I've assumed that I did not include the correct library as part of my VB6 distribution package or I'm having a problem developing on XP and distributing to Vista. Works great on my Vista system with VB6 loaded. Have you found a solution?