+ Reply to Thread
Results 1 to 2 of 2

Get the filename of selected file

Hybrid View

  1. #1
    Registered User
    Join Date
    08-13-2020
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    78

    Get the filename of selected file

    I use the following code to select a file and return its name

    It works fine, but when NO file is selected returns an error.

    Is there a way to amend the code so that the code will return a message saying something like No file is selected.

    Function FileOpenDialogBox()
    
    'Display a Dialog Box that allows to select a single file.
    'The path for the file picked will be stored in fullpath variable
     
      With Application.FileDialog(msoFileDialogFilePicker)
            'Makes sure the user can select only one file
            .AllowMultiSelect = False
            'Filter to just the following types of files to narrow down selection options
            .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
            'Show the dialog box
            .Show
            
            'Store in fullpath variable
            fullpath = .SelectedItems.Item(1)
            FileOpenDialogBox = fullpath
        End With
    
    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,186

    Re: Get the filename of selected file

    Try this version;

    Function FileOpenDialogBox()
    '    Display a Dialog Box that allows to select a single file.
    '    The path for the file picked will be stored in fullpath variable
     
        With Application.FileDialog(msoFileDialogFilePicker)
              'Makes sure the user can select only one file
              .AllowMultiSelect = False
              'Filter to just the following types of files to narrow down selection options
              .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
              'Show the dialog box
              If .Show = -1 Then
                  'Store in fullpath variable
                  fullpath = .SelectedItems.Item(1)
              Else
                  fullpath = "No file selected!"
              End If
        End With
        
        FileOpenDialogBox = fullpath
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Get selected filename without extension
    By sanjuss2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-18-2020, 03:10 AM
  2. Help adjusting macro that matches filename/exports file as PDF with filename
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2018, 10:58 AM
  3. Need to play a wav file based on path and filename in a selected cell using macro: SOLVED!
    By needtoregistersux in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-31-2016, 07:47 PM
  4. Replies: 4
    Last Post: 10-23-2013, 12:54 AM
  5. Specify pdf filename if pdf printer was selected
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2013, 04:37 PM
  6. HOW: Open Browse Window, Select File, Copy Filename Selected.
    By monak83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2012, 03:43 AM
  7. Using a Selected filename in a cell formula
    By jmb77 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-19-2010, 02:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1