Solved

Get file name an path using VBA

Posted on 2014-09-25
4
304 Views
Last Modified: 2014-09-26
I have a browse button on an Excel form so user can select a file to load.  The code below works great in Excel 2010, but causes a run-time error for users of Excel 2013.

Public Function ChooseFile() As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Show Windows Dialog box for users to choose an Item file.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
    .AllowMultiSelect = False
    ' Set the title of the dialog box.
    .Title = "Please select the file."
    ' Clear out the current filters, and add our own.
    .Filters.Clear
    .Filters.Add "All Files", "*.*"

    ' Show the dialog box. If the .Show method returns True, the
    ' user picked at least one file. If the .Show method returns
    ' False, the user clicked Cancel.
    If .Show = True Then
    ChooseFile = .SelectedItems(1)
    txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
    Debug.Print "From dialog " & txtFileName

    End If
    End With
End Function

Open in new window

0
Comment
Question by:rrhandle8
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:frankhelk
frankhelk earned 167 total points
ID: 40345547
Hmmm ... looks valid ... what's the error message ?
0
 
LVL 12

Accepted Solution

by:
James Elliott earned 333 total points
ID: 40345611
Try taking out the 'office' part of office.filedialog.
0
 
LVL 12

Assisted Solution

by:James Elliott
James Elliott earned 333 total points
ID: 40345615
Also try .selecteditems(0)
0
 

Author Closing Comment

by:rrhandle8
ID: 40346115
The problem was a reference to a grid control which did not exist on his computer.  Why it showed up when I tried to browse for files is strange.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question