Hello all,
I am getting a compile error, "User defined type not defined" on the following code, on the Dim fDialog As Office.FileDialog line. I got this code from a video to open a file dialog window to search for a file. I have tried to different versions of the code from 2 different sources and get the same error on the same line. What am I missing?
Private Sub cmdAddLogo_Click()
On Error GoTo SubError
'Add "Microsoft Office 16.0 Object Library" in references
Dim fDialog As Office.FileDialog
Dim varFile As Variant
txtSelectedName = ""
' Set up the File Dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = "Choose the Image you would like to Use"
.AllowMultiSelect = False
' .InitialFileName = "C:\Users\ray\Desktop\Export to Excel" 'Folder picker needs trailing slash
.Filters.Clear
' .Filters.Add "Excel files", "*.xls*"
' .Filters.Add "Excel files", "*.xls"
' .Filters.Add "Excel files", "*.xlsx"
' .Filters.Add "Excel macro-enabled", "*.xlsm"
If .Show = True Then
If .SelectedItems.Count = 0 Then
'User clicked open but didn't select a file
GoTo SubExit
End If
'An option for MultiSelect = False
'varFile = .SelectedItems(1)
'txtSelectedName = varFile
'Needed when MultiSelect = True
For Each varFile In .SelectedItems
txtSelectedName = txtSelectedName & varFile & vbCrLf
Next
Else
'user cancelled dialog without choosing!
'Do you need to react?
End If
End With
SubExit:
On Error Resume Next
Set fDialog = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub
Thanks in Advance