excelvbscriptfileopendialog

How to open file using Open File Dialog


I've been searching over the web for solution on how to open file using Open File Dialog in VBScript. Can someone point me on the right track?

My code below opens Excel file but I wanted it to be more dynamic in terms that the input file name can be change and not hard coded.

Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = 0
Set objShell = WScript.CreateObject("WScript.Shell")
path = objShell.CurrentDirectory
inFileName = "InputFile.xlsx"
inFilePath = path + "\" + inFileName

'Open target workbook
Set objWorkbook1 = objExcel.Workbooks.Open(inFilePath, False, True)
MsgBox "Reading Data from " & inFileName & vbNewLine, vbOkOnly + vbInformation, _
       "Reading Data"

Solution

  • When in doubt, read the documentation.

    Set dialog = objExcel.FileDialog(3)
    dialog.AllowMultiSelect = True
    dialog.Show
    
    For Each f In dialog.SelectedItems
      Set objWorkbook = objExcel.Workbooks.Open(f)
      '... do stuff ...
      objWorkbook.Close
    Next