7 Replies Latest reply on Apr 13, 2013 1:57 AM by Patrick.Cashman

    How to display a file selecting dialog on Windows7

    olsmir

      Hello All.

       

      On WindowsXP, when I need to display a file selecting dialog, I use the following object: Set objDialog = CreateObject("UserAccounts.CommonDialog").

      On Windows7, this doesn't work.

       

      I would like to hear from Windows7 users: what object do they use when they need to display a file selecting dialog?

       

      Oleg

        • 1. Re: How to display a file selecting dialog on Windows7
          Satoru

          Hi Oleg-san,

           

          UserAccounts.CommonDialog can't be used on Windows7.

          There is a type which uses an InternetExplorer object.

           

          Regards,

          Satoru

          1 of 1 people found this helpful
          • 2. Re: How to display a file selecting dialog on Windows7
            olsmir

            Hi Satoru,

             

            Thank you for this method. But comparing with the UserAccounts.CommonDialog method, your method is rather cumbersome construction. It requires a lot of excessive statements.

            I expected some approach built in Windows 7.

            However, it does work.

             

            Oleg

            • 3. Re: How to display a file selecting dialog on Windows7
              Satoru

              Hi Oleg-san,

               

              If it is a type which uses Excel, there is also a little easy construction.

              All the samples which work on Win7 which I have are sent.

               

              The method of using "MSComDlg.CommonDialog" works on a 32-bit command prompt.

              And if VB6 is not installed, vb6controls.reg needs to be installed.

               

              Regards,

              Satoru

              • 4. Re: How to display a file selecting dialog on Windows7
                olsmir

                Hi  Satoru,

                 

                Thank you very much for multiple methods you have sent me. I will certainly use some of them.

                 

                Oleg

                • 5. Re: How to display a file selecting dialog on Windows7
                  avjohn

                  I found some code that uses Microsoft Word to call a file dialog.  In our environment, everyone has MS Office installed.  It works well on Windows 7.


                  Function fBrowseForFile(sBrowsePath) 'sBrowsePath is the directory to start in.

                     ' This function opens a common file browsing dialog box

                     ' pointed at the local C:\ drive.  When a file is selected

                     ' this function will return the path and filename in a string.

                     ' If cancel is clicked or the browse window is closed without

                     ' a file being selected, the function returns an empty string.

                      Dim sBrowseFilter, oBrowseDialog

                      Dim objWord

                      Dim objFile

                      Dim objReturnFile

                      'set the type of dialog box you want to use

                      '1 = Open

                      '2 = SaveAs

                      '3 = File Picker

                      '4 = Folder Picker

                      Const msoFileDialogOpen = 1

                      Set objWord = CreateObject("Word.Application")

                      objWord.ChangeFileOpenDirectory(sBrowsePath)

                      With objWord.FileDialog(msoFileDialogOpen)

                          .Title = "Select the file to process"

                          .AllowMultiSelect = False

                          .Filters.Clear

                          .Filters.Add "All Files", "*.*"

                          If .Show = -1 Then

                              For Each objReturnFile In .SelectedItems

                                  Set objFile = objFso.GetFile(objReturnFile)

                                  fBrowseForFile = objFile.Path

                              Next

                          Else

                          End If

                      End with

                   

                      objWord.Quit

                  End Function

                  • 6. Re: How to display a file selecting dialog on Windows7
                    andrew_french

                    I personally like visual studio for these kinds of things, makes life a lot easier.  But just my personal preference.

                     

                    vb.net

                    Dim openfiledialog1 As New OpenFileDialog

                    Dim result As DialogResult = openfiledialog1.ShowDialog()

                     

                    C#.net

                    OpenFileDialog openFileDialog1= new OpenFileDialog;

                    DialogResult result = openFileDialog1.ShowDialog();

                    • 7. Re: How to display a file selecting dialog on Windows7
                      Patrick.Cashman

                      I also use vb.net for this.  It's super easy.

                       

                      1. Add the open file dialog object to the form.

                      2. Use code something like this:

                       

                      Main_Form.dlg_open.Title = title

                      Main_Form.dlg_open.FileName = ""

                      file = ""

                      if Main_Form.dlg_open.ShowDialog() = dialogresult.OK then

                      file = Main_Form.dlg_open.FileName

                      End If

                      3. The result is that 'file' captures the filename of the file to open.

                       

                      Oh and by the way the same code works equally well in XP or W7.