Excel

GetOpenFilenameFrom function

Ease of Use

Easy

Version tested with

2000, 2002, 2003 

Submitted by:

Ken Puls

Description:

This code was written to make it easier to use the GetOpenFilename function by having it open in a user specified directory. 

Discussion:

This code can be useful if you want to send the user to specific locations to open a workbook. Rather than just pop open a window in whatever directory they currently have as default, you can easily feed this function a directory to open at. The function can also be easily modified to accept the GetOpenFilename arguements to only show certain types of files as well. This function is best suited to those applications where you may need to open a variety of files from varying folders. NOTE: If the directory is invalid, it returns the current directory. 

Code:

instructions for use

			

Option Explicit Function GetOpenFilenameFrom(Optional sDirDefault As String) As Variant 'Macro Purpose: To ask for a file at a specified directory 'Returns either the full path including the filename, 'or False if nothing selected Dim sDirCurrent As String, _ sFileTypes As String 'Set the file types desired to show in the dialog box sFileTypes = "All Files (*.*),*.*" 'For specific file types, comment the line above, uncomment & adjust the line below 'Help on the exact syntax can be found by looking up the GetOpenFilename method in 'the VBA help files 'sFileTypes = "Excel Worksheets (*.xls), *.xls,Word Documents (*.doc), *.doc," _ & "All Files (*.*),*.*" 'Make note of the current directory sDirCurrent = CurDir If sDirDefault = vbNullString Then 'If optional arguement not supplied then 'assign current directory as default sDirDefault = CurDir Else 'If option arguement is supplied, test path to ensure 'that it exists. If not, assign current directory If Len(Dir(sDirDefault, vbDirectory)) = 0 Then sDirDefault = sDirCurrent End If End If 'Change the drive and directory '*Drive change is unecessary if same, but takes as long to test ' as just changing it ChDrive Left(sDirDefault, 1) ChDir (sDirDefault) 'Get the file's name & path GetOpenFilenameFrom = Application.GetOpenFilename(sFileTypes) 'Change the drive and directory back ChDrive Left(sDirCurrent, 1) ChDir (sDirCurrent) End Function Sub Test() 'Macro Purpose: To test the GetOpenFilenameFrom function Dim sWBToOpen As Variant sWBToOpen = GetOpenFilenameFrom(Range("A3").Value) If Not sWBToOpen = False Then Workbooks.Open (sWBToOpen) End Sub

How to use:

  1. Copy above code.
  2. In Excel press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save workbook before any other changes.
 

Test the code:

  1. Enter a file path in cell A3.
  2. Press Alt+F8 to display the macro dialog box.
  3. Choose Test and click okay.
  4. If your file path is valid, the dialog will open at that directory.
  5. To test from a macro open the VBE and edit "Range("A3").Value" in the test procedure to be the name of the directory you want (surrounded by quotes).
 

Sample File:

getopenfilenamefrom.zip 9.05KB 

Approved by mdmackillop


This entry has been viewed 264 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express