1

I am working on a code in Excel VBA, where the user can pick the default route of a file if it is not on its default path.

I would like to filter it, to make sure that they will not select a wrong file.

My idea is to filter it somehow to check if the "original filename" (that you can see in properties -> details) is the same as the one I give. This way it would work even if the specific file is renamed.

My problem is, that I don't know how to refer to it.

Edit

Thanks to ZAT, the actual code looks like this:

Private Sub vncexe(vncexe As String)

Dim vncpath1 As String
Dim vncpath2 As String
Static temppath As String
vncpath1 = "C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe"
vncpath2 = "C:\Program Files\RealVNC\VNC4\vncviewer.exe"

Dim opt As String
ob opt

If opt = "ob1" Then
    If Dir(vncpath1) <> "" Then
        vncexe = vncpath1
    ElseIf Dir(vncpath2) <> "" Then
        vncexe = vncpath2
    ElseIf temppath <> "" Then
        vncexe = temppath
    Else
        MsgBox "VNC viewer exe not on default path"
start:
        With Application.FileDialog(msoFileDialogFilePicker)
            .Title = "Please select VNC viewer"
            .InitialView = msoFileDialogViewSmallIcons
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "VNCviewer.exe", "*.exe"
            .Show
                If .SelectedItems.Count <> 1 Then 'here should the "OR <> [original filename]" be
                    End
                Else
                    vncexe = .SelectedItems(1)
                    strVNC = Right(vncexe, 13)
                        If strVNC = "vncviewer.exe" Then
                            temppath = vncexe
                        Else
                            MsgBox "wrong file selected"
                            temppath = ""
                            GoTo start
                        End If
                End If
        End With

        End If
    End If

End Sub

The original file path is already set to a default "vncpath1" and "vncpath2".

temppath is a string that gets the new path that we set manually here with this script if the file was not found on "vncpath1" and "vncpath2"

but my question was, if there is a way, to get the "original filename" of the selected exe and filter it, so it would work only if it is (in this case) "vncviewer.exe"

so even if I rename the file, the "original filename" property remains "vncviewer.exe"

thanks again to ZET the only problem with the code now is if the vncviewer.exe was renamed for example to vnc.exe, it will not work, this is why I need to get the "original filename" property.

And because I like to work fancy :-)

Community
  • 1
  • 1
Divin3
  • 538
  • 5
  • 12
  • 27

1 Answers1

1

Try this:

Sub filefilterdf()
Dim strVNC As String, vncexe As String
Dim vncpath1 As String
Dim vncpath2 As String
Static temppath As String
vncpath1 = "C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe"
vncpath2 = "C:\Program Files\RealVNC\VNC4\vncviewer.exe"

'If Dir(vncpath1) <> "" Then
'    vncexe = vncpath1 & " "
'ElseIf Dir(vncpath2) <> "" Then
'    vncexe = vncpath2 & " "
'ElseIf temppath <> "" Then
'    vncexe = temppath
'Else
'    MsgBox "VNC viewer exe not on default path"
'End If
''''Adjust placement of below code within above loop as per your need.


start:
With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Please select VNC viewer"
    .InitialView = msoFileDialogViewSmallIcons
    .AllowMultiSelect = False
    .Filters.Clear
    .Filters.Add "VNCviewer.exe", "*.exe"
    .Show
        If .SelectedItems.Count <> 1 Then 'here should the "OR <> [original filename]" be
            End
        Else
            vncexe = .SelectedItems(1) '& " "       'dont know why you used space here
            strVNC = Right(vncexe, 13)              'this is the key part
            'MsgBox strVNC
                If strVNC = "vncviewer.exe" Then
                    temppath = vncexe
                    'MsgBox temppath
                Else
                    MsgBox "wrong file selected"
                    temppath = ""
                    GoTo start
                End If
        End If
End With
End Sub
ZAT
  • 1,347
  • 7
  • 10
  • the space was used, because the exe is opened with an additional information. For example "C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe 172.21.1.1" this will make the vnc connect to the given ip address. I will try this ASAP! Thank you! – Divin3 Nov 04 '14 at 14:43
  • @Divin3 if purpose served upvote and/or mark as answer to close it. – ZAT Nov 05 '14 at 07:22
  • I give you the upvote because this solved most of the problems, but still it does only checks the files name, not the "original filename". That means if I rename the file, to "vncviewerrrrr.exe" it will not work. This is why I asked if there is a way to check for the "original filename" property. Thank you for your help – Divin3 Nov 05 '14 at 16:18
  • when you change filename to vncviewerrrr.exe should you not change the filter accordingly? If yes, then you can use that also to change strVNC string accordingly. And, what property name are you referring? After right click on any file > properties > detail i dont see any name. – ZAT Nov 05 '14 at 17:12
  • I can not add a screenshot because my companys policy does not allow me to, and I am at work at the moment, but what I am talking about is when you right click a file, than properties -> Details -> in the Description the last detail is the "Original filename" this is what I would like to refer to, because this remains the same even if we change the name of the file. – Divin3 Nov 05 '14 at 19:13