FileDialog Error

SimoneRene

Registered User.
Local time
Today, 17:28
Joined
Mar 15, 2017
Messages
58
Hi!

So I'm trying to use FileDialog on a form, I have a created an event OnClick of a button and have a Text Box to display the result.

Whenever I click said button, I get a Run-time error '13' Type Mismatch Debug takes me to this line of code:

Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

I'm running Access 16, 64-Bit System. I have set up a reference.

Any help greatly appreciated, I'm an access/VBA newbie.

Thanks!
 
I expect you have fDialog dimmed as the wrong type of object.
 
Thanks for your reply Galaxiom,

Here is the code:

Private Sub OpenDialogBtn_Click()
'On Error GoTo SubError (Suspended for Debug)

'Add Microsoft Office Object Library in References
Dim fDialog As Office.FileDialog
Dim varFile As Variant

FileViewerTxt = ""

'Set up the File Dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog
.Title = "Choose the Drawing you would like to import"
.AllowMultiSelect = False

.Filters.Clear
.Filters.Add "PDF Files", "*.pdf*"

If .Show = True Then
If .SelectedItems.Count = 0 Then
'If count is 0 no file was selected - Add Code to create notification that no file was selected (MsgBox...)
GoTo SubExit
End If

For Each varFile In .SelectedItems
FileViewerTxt = FileViewerTxt & varFile & vbCrLf
Next

Else
'User Cancelled dialog without Choosing
End If


What would you suggest I try?

I don't know much about object types, am I asking it to find groceries in a shoe shop?

Thanks!


Thank you.
 
I'm not sure and the search might be a bear to confirm what type of object you should have used. I've had success with allowing the use of a generic object. This is the equivalent of using a Variant data type for non-object variables. MAYBE...

Code:
Dim fDialog As Object
 
Hi The_Doc_Man,

Thank you very much!!

Dim fDialog As Object has done the trick. :)
 
Mysterious.
Dim fDialog As Office.FileDialog should work.

Dim As Object is a generic solution that doesn't support Intellisense.

You could try not specifying the library.
Code:
Dim fDialog As FileDialog
 
Actually, Greg, I agree! But the quick answer got her off the dime. Like I said, it might be a real bear to figure out why the explicit library reference didn't work.

SimoneRene, if you happen to check this, I want to be clear that I gave you a quick and dirty answer only because I wanted to get you up and going. However, Galaxiom is right that what you did first should have worked OR should have given you a compile-time error. I'm guessing some problem with libraries but it would take a while to track it down and the process would be tedious.
 
Actually, Greg, I agree! But the quick answer got her off the dime.

For sure. Many are the times when I have not been able to work out what something should be dimmed as and gone WTF, Dim As Object, so I can get on with the code. My dealings in Groupwise automation (:eek::() come to mind. I always promise to go back later and fix it. ;)

While dropping the library name usually does work, I prefer to be specific because it helps avoid unwelcome surprises when two referenced libraries happen have the same members.

Like I said, it might be a real bear to figure out why the explicit library reference didn't work.

It certainly wouldn't be easy to troubleshoot. Clearly the reference to the library is included or it wouldn't work at all. Quite odd.

Office Repair Install would be my first stop but I wouldn't be too hopeful. I would be curious to know of other troubleshooting suggestions.
 
Mysterious.
Dim fDialog As Office.FileDialog should work.

Dim As Object is a generic solution that doesn't support Intellisense.

You could try not specifying the library.
Code:
Dim fDialog As FileDialog

Weridly "Dim fDialog As FileDialog" didn't work, same Error; Type Mismatch Error 13.
 
Simone,

FWIW,I took your code, added the code in red to get it to compile/run?
I then added Microsoft Office 15.0 Object Library as I was not using it, and the code runs fine?

It shows the file window, I select a file and that appears in 'FileViewerTxt'?

HTH

Code:
Private Sub TestDialog()


'On Error GoTo SubError (Suspended for Debug)

'Add Microsoft Office Object Library in References
Dim fDialog As Office.FileDialog
Dim varFile As Variant

FileViewerTxt = ""

'Set up the File Dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog
.Title = "Choose the Drawing you would like to import"
.AllowMultiSelect = False

.Filters.Clear
.Filters.Add "PDF Files", "*.pdf*"

If .Show = True Then
If .SelectedItems.Count = 0 Then
'If count is 0 no file was selected - Add Code to create notification that no file was selected (MsgBox...)
GoTo SubExit
End If

For Each varFile In .SelectedItems
FileViewerTxt = FileViewerTxt & varFile & vbCrLf
Next

Else
'User Cancelled dialog without Choosing
End If
[COLOR="Red"]End With
SubExit:[/COLOR]
End Sub
 
Simone,

FWIW,I took your code, added the code in red to get it to compile/run?
I then added Microsoft Office 15.0 Object Library as I was not using it, and the code runs fine?

It shows the file window, I select a file and that appears in 'FileViewerTxt'?

HTH

Code:
Private Sub TestDialog()


'On Error GoTo SubError (Suspended for Debug)

'Add Microsoft Office Object Library in References
Dim fDialog As Office.FileDialog
Dim varFile As Variant

FileViewerTxt = ""

'Set up the File Dialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog
.Title = "Choose the Drawing you would like to import"
.AllowMultiSelect = False

.Filters.Clear
.Filters.Add "PDF Files", "*.pdf*"

If .Show = True Then
If .SelectedItems.Count = 0 Then
'If count is 0 no file was selected - Add Code to create notification that no file was selected (MsgBox...)
GoTo SubExit
End If

For Each varFile In .SelectedItems
FileViewerTxt = FileViewerTxt & varFile & vbCrLf
Next

Else
'User Cancelled dialog without Choosing
End If
[COLOR="Red"]End With
SubExit:[/COLOR]
End Sub

Yesssss!!!!

After your suggestion I moved "End With" to before the Sub Exit/Sub Error part of the code.
Success, "Dim fDialog As Office.FileDialog" now works! =] Thanks!!
 

Users who are viewing this thread

Back
Top Bottom