



' Use the FileDialog_GetFile method to Display the file open dialog and retrieve the file name ' This method accepts parameters rather than requiring the class properties to be set. We might need a button in excel to open windows file dialog box so as to select the. Option Explicit Private Sub CommandButton1Click () Dim directory As String, fileName As String, sheet As Worksheet, total As Integer Dim fd As Office.FileDialog Set fd Application.FileDialog (msoFileDialogFilePicker) With fd. Figure C Once you select the file and click Open, the dialog box closes, and the full path of the file. 2) In the VBA code 2, please change the folder path in the following line to the. ' Support VB6 and VBA with 32 and 64 bit Windows API calls.Ĭonst cstrDefaultFile As String = "C:\Total Visual SourceBook 2013\Samples\example.txt" Sometimes, we may need to work with external files in Microsoft Excel. Click the Command button to open the File Open dialog box, as in Figure C. 1) The VBA code 1 helps to open the default Documents folder in your computer. ' Comments: Examples of using the modFileDialog module to invoke the Windows API Common Control for selecting a file or files from the File Dialog. ' Then run the procedure by putting the cursor in the procedure and pressing: ' F5 to run it, or ' F8 to step through it line-by-line (see the Debug menu for more options) you would have chosen File I> Open to display that dialog box. ' Example of modFileDialog ' ' To use this example, create a new module and paste this code into it. Some applications, such as Word and Excel, let you use their. For example, to refer to the Open dialog box, you use the constant wdDialogFileOpen, because in previous versions of Word, you would have chosen File.
