Solved

Get file name an path using VBA

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

863 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

21 Experts available now in Live!

Get 1:1 Help Now