• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

Dedicated Dad, Software Engineer and Lover of Coffee

SOLVED: Late Binding File Dialog in VBA Example

November 20, 2015 by Ryan McCormick 4 Comments

I have been trying to figure out a solution to late binding the file dialog object for a long time. I have played with the code, googled for examples and haven’t had much luck until today when a generous visitor of ours posted the solution in the comments of our Select or Open a file in VBA with file dialog post.

The original FileDialog post referenced above shows an example of how to use the FileDialog object with the requirement of adding a reference to the Microsoft XX.X Office Object Library. When using late binding, no reference is needed. The code just works.

Our visitor’s comment of the example originated from an accepted answer on a StackOverflow thread: http://stackoverflow.com/questions/1091484/how-to-show-open-file-dialog-in-access-2007-vba. For the purposes of this post, I modified the StackOverflow example to better handle when users cancel out of FileDialog. This example works across all office products (Word, Excel, Access, Outlook, etc…) and Office versions 2007, 2010, 2013.

Late Binding File Dialog in VBA

'--------------------------------------------------
' File Browse Code
'--------------------------------------------------
' NOTE: Late binding version needs no
' MS Office Object Library Reference
'--------------------------------------------------
Function selectFile()
    Dim fd As Object
    Set fd = Application.FileDialog(3)
    
    With fd
        If .Show Then
            selectFile = .SelectedItems(1)
        Else
            'stop execution if nothing selected
            End
        End If
    End With
    
    Set fd = Nothing
End Function

For example-sake, here is the example that uses early binding:

Early Binding File Dialog Example With VBA

'--------------------------------------------------
' File Browse Code
'--------------------------------------------------
'NOTE: To use this code, you must reference
'The Microsoft Office 14.0 (or current version)
'Object Library by clicking menu Tools>References
'Check the box for:
'Microsoft Office 14.0 Object Library in Access 2010
'Microsoft Office 15.0 Object Library in Access 2013
'Click OK
'--------------------------------------------------
Function selectFile()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        If .Show Then
            selectFile = .SelectedItems(1)
        Else
            'stop execution if nothing selected
            End
        End If
    End With
    
    Set fd = Nothing
End Function

As mentioned above, this is something that has been driving me crazy for a long time. The reason this example works is because of the enumeration of msoFileDialogFilePicker to 3. Without the MS Office XX.X Object Library reference, the language interpreter doesn’t know what msoFileDialogFilePicker means.

Please comment with any suggestions, improvements, banter, etc…

Related

Filed Under: VBA Tagged With: file dialog, late binding, MS Access, ms excel, vba

Reader Interactions

Comments

  1. Cesar Troya Sherdek says

    May 15, 2018 at 6:13 pm

    Hello, I was trying your solution, I’m interested in observing the resulting text string, so my code is:

    Private Sub Command2_Click()
    Dim fd As Object
    Dim directorio As String
    Set fd = Application.FileDialog(3)

    With fd
    If .Show Then
    selectFile = .SelectedItems(1)
    Else
    ‘stop execution if nothing selected
    End
    End If
    End With
    directorio = selectFile
    MsgBox (selectFile)

    Set fd = Nothing
    End Sub

    but it gives me the following error, highlighting “selectFile”: Cant Find project or library

    Reply
    • Ryan McCormick says

      May 15, 2018 at 8:06 pm

      The example used a function called selectFile where selectFile is set as the return value. I haven’t tried this out to confirm, but you could try this:

      Private Sub Command2_Click()
        Dim fd As Object
        Dim directorio As String
        Set fd = Application.FileDialog(3)
      
        With fd
          If .Show Then
            directorio = .SelectedItems(1)
          Else
            ‘stop execution if nothing selected
            End
          End If
        End With
      
        MsgBox (directorio)
      
        Set fd = Nothing
      End Sub
      
      Reply
  2. Jauber says

    February 5, 2019 at 7:02 am

    Great solution, i searching how to use objects without references! Thanks man! Great explanation. Works wonderful.

    Reply
  3. nicki says

    February 19, 2020 at 2:06 pm

    great. It works for me. I am using widown10 and MS Access2013. I have office object file 15.0 selected but still not working with late binding it worked. thank you so much.

    Reply

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  • KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  • jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  • Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  • mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  • Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Archives

  • May 2019
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • March 2017
  • December 2015
  • November 2015
  • July 2015
  • April 2015
  • February 2015
  • September 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • October 2013
  • August 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • December 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • May 2012
  • March 2012
  • February 2012
  • December 2011
  • November 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2009
  • July 2009
  • May 2009

Categories

  • Angular
  • Angular 2
  • AngularJS (1x branch)
  • Computer Q&A
  • ES2015
  • Internet Marketing
  • Javascript
  • Job Interviews
  • Job Search
  • Karma
  • Laravel
  • Linux
  • Linux/Unix Tips
  • MacOS
  • Microsoft Access
  • Microsoft Excel
  • Microsoft Outlook
  • Microsoft Word
  • News
  • Node
  • Open Source
  • PHP
  • Protractor
  • Resume Writing
  • Spring Boot
  • SQL
  • Ubuntu
  • VBA
  • VBScript
  • VirtualBox
  • Web Development
  • Windows Tips
  • Wordpress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in