How do I prevent users from selecting an Internet Shortcut on a File Dialog?

Magic Polygon

New Member
Joined
Aug 20, 2023
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
I have allowed only *.bmp, *.cur, *.gif, *.ico, *.jpg, and *.wmf file formats through filtering, but Internet Shortcuts are visible and selectable in the File Dialog. I don't know how to hide out the Internet shortcuts or reprompt the user for a valid choice of file.

VBA Code:
Private Sub UploadImageCommandButton_Click()

    'Declare a variable as a FileDialog object
    Dim UploadPictureFileDialog As FileDialog

    'Path to the image
    Dim ImagePath As String
    
    'Create a FileDialog object as a File Picker dialog box
    Set UploadPictureFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    'Reference the File Dialog object
    With UploadPictureFileDialog
    
        'Remove all filters currently applied to the file dialog box
        .Filters.Clear
        
        'Sets the title of the file dialog box displayed
        .Title = "Select a Photo"
        
        'Adds a filter to the file dialog box at position 1 of the list of filters
        .Filters.Add "Images", "*.bmp; *.cur; *.gif; *.ico; *.jpg; *.wmf", 1
        
        'Only a single file can be selected from the dialog box
        .AllowMultiSelect = False
        
    End With
    
        'Use the Show method to display the File Picker dialog box and return the user's action
        'The user pressed the button
        If UploadPictureFileDialog.Show = -1 Then
        
            'Store the path of the selected item
            ImagePath = UploadPictureFileDialog.SelectedItems(1)

            'Fit the image into the picture frame
            UploadedImage.Picture = LoadPicture(ImagePath)
            UploadedImage.PictureSizeMode = fmPictureSizeModeZoom
        
        'The user pressed Cancel
        Else
        End If

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Can you post examples of what the url or path of your "internet shorcuts" look like? Or maybe what I'm thinking will provide direction:
VBA Code:
If Instr("ImagePath","http")<> 0 Then
   UploadedImage.Picture = LoadPicture(ImagePath)
   UploadedImage.PictureSizeMode = fmPictureSizeModeZoom
Else
   Do something else here
End If
 
Upvote 1
For example this appears,

Internet Shortcut not filtered.png



C:\Users\IvanQ\OneDrive\Desktop\Darkest Dungeon®

which also has the URL

steam://rungameid/262060
 
Upvote 0
An internet shortcut contains a url property. A shortcut to OneDrive does not, but it does contain the string "OneDrive".
So use what I showed and test for both. If either is found, prompt and exit.

VBA Code:
If UploadPictureFileDialog.Show = -1 Then
   'Store the path of the selected item
   ImagePath = UploadPictureFileDialog.SelectedItems(1)
   If Instr(1,"ImagePath","http") <> 0 Or Instr(1,"ImagePath", "OneDrive") <> 0 Then
      MsgBox "your message here"
      Exit Sub
   End If
'rest of your code follows
 
Upvote 1
Thanks for sharing this. Using what you provided, I came with the ugly code below:

VBA Code:
'Use the Show method to display the File Picker dialog box and return the user's action
'The user pressed the button
If UploadPictureFileDialog.Show = -1 Then
        
    'Store the path of the selected item
    ImagePath = UploadPictureFileDialog.SelectedItems(1)
            
    'If any of the valid file name extensions do not appear...
    If InStr(1, ImagePath, ".bmp") = 0 _
    And InStr(1, ImagePath, ".cur") = 0 _
    And InStr(1, ImagePath, ".gif") = 0 _
    And InStr(1, ImagePath, ".ico") = 0 _
    And InStr(1, ImagePath, ".jpg") = 0 _
    And InStr(1, ImagePath, ".wmf") = 0 _
    Then
                
        '...inform the user...
        MsgBox _
        "You have to select a file of one of the following file formats:" + Chr(10) + "*.bmp, *.cur, *.gif, *.ico, *.jpg, *.wmf"
                
        '...and return to the userform
        Exit Sub
    End If
'rest of my code follows
 
Upvote 0
Solution
Why would you test for 6 when you can test for 2? Doing it your way, you could build an array of file extensions and loop over it, passing each array element to Instr. It would be just about the same number of code lines altogether, but more 'professional' for lack of a better word.
 
Upvote 0
I chose to test 6 file extensions because the Image control only supports those six extensions, and I wanted to allow for as much valid flexibility as possible for the user. At the time, I was too lazy to learn how to do arrays, but I think the code below is an improvement.

VBA Code:
'Some omissions have been made for brevity
'Declarations
Dim UploadPictureFileDialog As FileDialog
Dim SupportedFileFormats As Variant 'Represents an array of 6 Strings
Dim Format As Variant 'Represents a supported file format
'Create an array of supported file formats
SupportedFileFormats = Array(".bmp", ".cur", ".gif", ".ico", ".jpg", ".wmf")
 
'Store the path of the selected item
ImagePath = UploadPictureFileDialog.SelectedItems(1)
        
'Going through each supported file format...
For Each Format In SupportedFileFormats
        
    '...if the current extension appears in the file name...
    If InStr(1, ImagePath, Format) <> 0 Then
            
        '...fit the image into the picture frame...
        UploadedImage.Picture = LoadPicture(ImagePath)
        UploadedImage.PictureSizeMode = fmPictureSizeModeZoom
    
        '...and return to the userform
        Exit Sub
                
    End If
            
Next Format
        
'At this point, none of the valid file name extensions appear
Msgbox "Bad format"
        
'Rest of my code follows
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top