Solved

Get file name an path using VBA

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

Assisted Solution

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

Accepted Solution

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

Assisted Solution

by:James Elliott
James Elliott earned 333 total points
Comment Utility
Also try .selecteditems(0)
0
 

Author Closing Comment

by:rrhandle8
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
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 …

762 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

7 Experts available now in Live!

Get 1:1 Help Now