Click here to Skip to main content
Licence MIT
First Posted 14 Oct 2001
Views 181,397
Downloads 4,618
Bookmarked 40 times

A Filebrowser for VBA

By | 3 Oct 2008 | Article
An Excel demo of how to build a File Browser in an Office application
Sample Image - FileBrowse.gif

Introduction

Here is an example of how you can build a common file browser in an Office application. I won't explain all about the Windows API functions used because it would be too much for this article. I made this for Excel, but you can use it with all VBA-aware applications. When you open the Excel file and look at the whole project in the VBViewer (ALT+F11), you will see that the main work is done by the Module called FileBrowser. You can export it and import it to another project.

Explanations of the Code

Declare Function th_apiGetOpenFileName Lib "comdlg32.dll" 
    Alias "GetOpenFileNameA" (OFN As thOPENFILENAME) As Boolean
Declare Function th_apiGetSaveFileName Lib "comdlg32.dll" 
    Alias "GetSaveFileNameA" (OFN As thOPENFILENAME) As Boolean
Declare Function CommDlgExtendetError Lib "commdlg32.dll" () As Long

The functions of GetOpenFileName are the secret to building this interface. For detailed information about these functions, please read an API guide. Note: OFN is the Container that allows you to browse files.

Function StartIt()
    Dim strFilter As String
    Dim lngFlags As Long
    strFilter = thAddFilterItem(strFilter, "Excel Files (*.xls)", "*.XLS")
    strFilter = thAddFilterItem(strFilter, "Text Files(*.txt)", "*.TXT")
    strFilter = thAddFilterItem(strFilter, "All Files (*.*)", "*.*")
    Startform.filenameinput.Value = thCommonFileOpenSave(InitialDir:=
        "C:\Windows", Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, 
        DialogTitle:="File Browser")
   Debug.Print Hex(lngFlags)
End Function

This is the main function. The thAddFilterItems are shown in the drop-down menu to select the different file types. If you want to add other items, e.g. Word Files (*.doc), copy and paste the line and fill in what you like. What's important is the InitialDir, which gives the path at which to start. The function thCommonFileOpenSave rules the dialog and returns as a string containing the selected file. This string is given to the textbox filenameinput of Startform. So, now you have the control to work with a file in a form chosen by the user. The function thCommonFileOpenSave looks difficult, but most of the time it is only setting properties of the OFN object.

If OpenFile Then fResult = th_apiGetOpenFileName(OFN) 
Else fResult = th_apiGetSaveFileName(OFN)
If fResult Then
    If Not IsMissing(Flags) Then Flags = OFN.Flags
        thCommonFileOpenSave = TrimNull(OFN.strFile)
    Else
        thCommonFileOpenSave = vbNullString
End If

This is the important part of the function thCommonFileOpenSave. The first line decides if you want to save or open a file, and calls the functions needed. The if-block statement defines the return value of the function. Either it returns OFN.strFile, which equals the name of the chosen file or a null string. The function TrimNull only kills Nullchars.

I hope the rest of the Module code is clear, but don't ask me about the setting of the hex-values. I found it in an API guide and I don't know what it does particularly. Also, please don't write about my bad English. I'm a student of physics and not a linguistic genius. Have fun with it.

History

  • 14th October, 2001: Initial post
  • 3rd October, 2008: Download file updated

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Hasler Thomas

Software Developer (Senior)
Mettler Toledo
Switzerland Switzerland

Member

Bachelor in Computer Science.
Works as a Software Engineer (C#) for Mettler Toledo in Switzerland.
Develops software for formulation and SQC.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMettler Toledo DLL PinmemberErnesto Herrera8:07 9 Feb '09  
GeneralRe: Mettler Toledo DLL PinmemberHasler Thomas22:00 10 Feb '09  
GeneralRe: Mettler Toledo DLL PinmemberTorrock14:07 25 Sep '09  
AnswerRe: Mettler Toledo DLL PinmemberHasler Thomas22:57 27 Sep '09  
GeneralGreat post Pinmemberpacroman10:45 25 Nov '08  
GeneralGreat, clear and easy to use Pinmemberkarljohnson13:42 7 May '08  
GeneralUse In Excel Spreadsheet PinmemberRM67665:18 13 Jul '06  
GeneralIt Actually works in Outlook ! Pinmemberlucaso13:42 27 Jan '06  
QuestionHow to customize the dialog box? PinmemberDoan Phu Huyen20:43 25 Sep '05  
QuestionOpening more than one file? Pinmembermacot39212:10 7 Jun '05  
AnswerRe: Opening more than one file? PinmemberHasler Thomas1:41 8 Jun '05  
GeneralRe: Opening more than one file? PinmemberHasler Thomas1:58 8 Jun '05  
GeneralRe: Opening more than one file? Pinmembermacot3926:26 8 Jun '05  
GeneralVSS Folder browser PinmemberAntony M3:01 19 Mar '04  
GeneralPlug-in for MS Outlook. please help PinmemberAtif Bashir18:45 30 Jul '03  
GeneralSpeed PinmemberLars Wiberg3:57 9 Jan '03  
GeneralRe: Speed PinmemberHasler Thomas1:14 16 Jan '03  
Generalcode in access Pinmemberami3:11 27 Jun '02  
QuestionRe: code in access Pinmemberthemanof835:31 30 Sep '08  
AnswerRe: code in access PinmemberHasler Thomas5:09 2 Oct '08  
AnswerRe: code in access PinmemberHasler Thomas23:45 2 Oct '08  
QuestionWhat about GetOpenFilename method? PinmemberAnonymous13:38 20 Jun '02  
AnswerRe: What about GetOpenFilename method? PinmemberHasler Thomas1:14 27 Aug '02  
QuestionRe: What about GetOpenFilename method? PinmemberLarry White9:14 14 Mar '06  

Good day,
 
I am trying to use your code from "A Filebrowser for VBA" for capturing a filename including path into a SQL database field. I am using a Microsoft Access Project as the front-end. I associated the code with the OnGotFocus event property and receiving this error:
 
Only comments may appear after End Sub, End Function, or End Property.
 
I changed the form and field name to my form and added 2 additional file types for variables.
 
Smile | :)
 

 
LWhite
GeneralNice Code, shame that you ripped it off! PinmemberAnonymous10:33 17 Feb '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 4 Oct 2008
Article Copyright 2001 by Hasler Thomas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid