Hello all,
I need a quick fix. I can't get my code to open the file. The file dialog opens but when I select the file nothing happens. What am I missing?
I need a quick fix. I can't get my code to open the file. The file dialog opens but when I select the file nothing happens. What am I missing?
Code:
Dim fd As FileDialog
Dim FileName As String
'Dim Workbooks As OfficeDataSourceObject
Set fd = Application.FileDialog(msoFileDialogOpen)
'the number of the button chosen
Dim FileChosen As Integer
FileChosen = fd.Show
fd.Title = "Choose workbook"
fd.InitialFileName = "C:\FilePath"
fd.InitialView = msoFileDialogViewList
'show Excel workbooks and macro workbooks
fd.Filters.Clear
fd.Filters.Add "Excel workbooks", "*.xls"
fd.Filters.Add "Excel macros", "*.xlsm"
fd.FilterIndex = 1
fd.ButtonName = "Choose this file"
If FileChosen <> -1 Then
'didn't choose anything (clicked on CANCEL)
MsgBox "No file opened"
Else
'get file, and open it (NAME property
'includes path, which we need)
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)
Range("C1") = "Cost elem"
Range("D1") = "Cost element descr"
Range("E1") = "Per"
Range("F1") = "Year"
Range("G1") = "RefDocNo"
Range("H1") = "User"
Range("I1") = "Name"
Range("J1") = "PartnerObj"
Range("K1") = "Purchdoc"
Range("L1") = "Descr"
Range("M1") = "Material"
Range("N1") = "Sales doc"
Range("O1") = "Partner order"
Range("P1") = "Postg date"
Range("Q1") = "Value COCurr"
Range("R1") = "Quantity"
Range("S1") = "Quantity2"
End If