64 Bit Open File Dialogue Box
To display an Open File Dialogue Box if you didn't add the control, the code is quite different. It's still fairly straightforward, though. Add the following to your coding window:
Dim fName As String
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = FilePath
.Filters.Clear
.Filters.Add "JPEGS", "*.jpg; *.jpeg"
.Filters.Add "GIF", "*.GIF"
.Filters.Add "Bitmaps", "*.bmp"
.AllowMultiSelect = False
If .Show = True Then
fName = .SelectedItems(1)
Else
MsgBox "Operation Cancelled"
Exit Sub
End If
End With
We're using a With Statement, here. But notice what comes after With:
Application.FileDialog(msoFileDialogOpen)
After the Application object, you can add a FileDialog method. In between round brackets, you need to add one of four available built-in constants. The four constants are these:
msoFileDialogFilePicker
msoFileDialogFolderPicker
msoFileDialogOpen
msoFileDialogSaveAs
The one we want is msoFileDialogOpen .
In between With and End With we first have this:
.InitialFileName = FilePath
The InitialFileName is like the InitDir above: it sets the folder that you initially want to start from.
Next, we set up some filters:
.Filters.Clear
.Filters.Add "JPEGS", "*.jpg; *.jpeg"
.Filters.Add "GIF", "*.GIF"
.Filters.Add "Bitmaps", "*.bmp"
This sets the type of files we want to display in the dialogue box. The first instruction clears the dropdown box, otherwise you'd end up with lots of other file types that you don't need. Next, we add the filters, one per line. The asterisk means "all files of this type".
The With Statement also sets AllowMultiSelect to False. The final thing it does is to place the selected image into the fName variable. But we need to do this in an If Statement:
If .Show = True Then
fName = .SelectedItems(1)
Else
MsgBox "Operation Cancelled"
Exit Sub
End If
To place the select image into the fName variable you use SelectedItems. The 1 in round brackets means the first selected image. If you had set AllowMultiSelect to True then you could have used a for loop to loop through each file that the user selected.
The Show part is the one that actually displays the dialogue box. If it has a value of True then a file was selected and the user clicked Open. If it has a value of False then the Cancel button was clicked instead, and we can exit the Sub.
Your coding window should look like this, though: