OscarT
OscarT

Reputation: 61

Same dialog box to select file OR folder in VBA

I have an Excel workbook that is supposed to be filled with file or folder paths in order to process them later. What I would like to do, is to use the same dialog box in order to select the desired paths, like if a file is selected, return its path, but if no file is selected, return the current folder path. Is it possible to do that in a relative easy way? Or would I need a prompt asking the user if he wants a file or folder before loading the browse window?

I can create a file browse dialog like: Application.FileDialog(msoFileDialogFilePicker)

Or a Folder browse dialog: Application.FileDialog(msoFileDialogFolderPicker)

But how can I combine the two of them? It's not mandatory to use Application.FileDialog if there is a better option. Thanks all!

Upvotes: 5

Views: 1470

Answers (2)

Cristian F.
Cristian F.

Reputation: 480

I managed to find an alternative for my procedure, reusing insert hyperlink window in vba editor. It automatically adds the hyperlink, so it's no need to reset it.

Application.Dialogs(xlDialogInsertHyperlink).Show

And then take the hyperlink if it's presented and do whatever you want :D

If Range(cell).Hyperlinks.Count > 0 Then
        Link = ActiveCell.Hyperlinks(1).Address
        Activecell.Hyperlinks(1).Delete ' Don't forget to unlink :D
End If

Advantages :

  • use it for web addresses
  • use it for a file
  • use it for a folder

Upvotes: 1

user3476534
user3476534

Reputation: 220

I would create a userform with just an input box, and when the user clicks it, call the filedialog box and fill it in with the output. However, if the user doesn't click it, and just hits "OK", then handle the if textbox = "" even with your default location. This required knowledge of userforms.

Upvotes: 1

Related Questions