'FileDialog' type is not defined in MS Access
Asked Answered
B

3

7

In Access 2016, I wish to display the File Open dialog, allowing users to select a CSV file to import. However, an error is being generated in relation to the line Dim FD as Office.FileDialog -

Compile error: User-defined type not defined

The below code has been copied (and edited slightly) from the example posted on MSDN. This example is listed as relevant for Office 2013 and later, yet the very first comment in the code (in relation to the variable type Office.FileDialog) seems to contridict this -

Requires reference to Microsoft Office 11.0 Object Library.

Surely for Office 2013 you'd need to refernece the MS Office 15 Object Library, and then the respective version library for future versions, such as 2016?

But regardless, in Access 2016 there is no refernece to the Microsoft Office 11.0 Object Library. There is however a reference to the Microsoft Access 16.0 Object Library, which is included.

How can I get the File Open dialog to show?

Function SelectFile(Optional ByVal title As String = "Please select a file", _
                    Optional ByVal allowMultiSelect As Boolean = False) As Variant

    Dim FD As Office.FileDialog
    Dim file As Variant

    Set FD = Application.FileDialog(msoFileDialogFilePicker)

    With FD

        .title = "Please select a file"         ' Add the dialog title
        .allowMultiSelect = allowMultiSelect    ' Set whether or not to allow multiple file selection

        .filters.Clear                      ' Clear any existing filters
        .filters.Add "CSV Files", "*.csv"   ' Add new filters

        '**
         ' Show the dialog to the user
         '*
        If .Show = True Then

            For Each file In .selectedItems  ' Grab the path/name of the selected file
                SelectFile = file
            Next

        Else
            SelectFile False
        End If

   End With

   Set FD = Nothing    ' Clean up the FD variable

End Function

Here are my currently selected references -

Selected references

And here are the available MS Office referencs (no refernece to Microsoft Office 16.0 Object Library) -

Available MS Office references

Bever answered 22/2, 2016 at 14:39 Comment(0)
F
8

I don't know why Microsoft Office [version] Object Library is not displayed among the available references. However, you don't need it if you switch to late binding.

Const msoFileDialogFilePicker As Long = 3
'Dim FD As Office.FileDialog
Dim FD As Object
Dim file As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)

Later on, you'll need to decide what to do here ...

For Each file In .selectedItems  ' Grab the path/name of the selected file
    SelectFile = file
Next

When you run that code with AllowMultiSelect = True, and select multiple files, SelectFile will contain only the last of them.

Forgetmenot answered 22/2, 2016 at 14:54 Comment(2)
Thanks, that works. I guess it's just a teething problem with 2016! And I see what you mean about multiple file selections. I think I'll just remove the option and set to false by default, as it's not needed in this instance (I was just trying a to forward plan a little).Bever
The problem is only post-poned to the next line : Application.FileDialog. More over FD is not Office.FileDialog but Application.FileDialog. You only need to add Microsoft office x.0 Object library included in your projectMyrmecology
J
16

For anyone having the same issue, you need to select the 'Microsoft Office 16.0 Object Library' which is not the same as 'Microsoft Access 16.0 Object Library'

Jerri answered 1/5, 2017 at 21:56 Comment(0)
F
8

I don't know why Microsoft Office [version] Object Library is not displayed among the available references. However, you don't need it if you switch to late binding.

Const msoFileDialogFilePicker As Long = 3
'Dim FD As Office.FileDialog
Dim FD As Object
Dim file As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)

Later on, you'll need to decide what to do here ...

For Each file In .selectedItems  ' Grab the path/name of the selected file
    SelectFile = file
Next

When you run that code with AllowMultiSelect = True, and select multiple files, SelectFile will contain only the last of them.

Forgetmenot answered 22/2, 2016 at 14:54 Comment(2)
Thanks, that works. I guess it's just a teething problem with 2016! And I see what you mean about multiple file selections. I think I'll just remove the option and set to false by default, as it's not needed in this instance (I was just trying a to forward plan a little).Bever
The problem is only post-poned to the next line : Application.FileDialog. More over FD is not Office.FileDialog but Application.FileDialog. You only need to add Microsoft office x.0 Object library included in your projectMyrmecology
H
0

In office 2016 the "Microsoft Office 16.0 Object Library" is missing from the references window. The main issue I was having was determining the location and name of the dll file.

Finally I found the full file path, which I imagine will be the same for any user because I believe it is a default directory:

"C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO.dll"

I have spent 1 hour to find this path just so I can create File Dialogs!

Hallowmas answered 22/8, 2017 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.