Solved

Get file name an path using VBA

Posted on 2014-09-25
4
300 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now