1

我正在处理 Excel VBA 中的代码,如果文件不在其默认路径上,用户可以在其中选择文件的默认路径。

我想过滤它,以确保他们不会选择错误的文件。

我的想法是以某种方式过滤它以检查“原始文件名”(您可以在属性-> 详细信息中看到)是否与我给出的相同。这样,即使特定文件被重命名,它也可以工作。

我的问题是,我不知道如何引用它。

编辑

感谢 ZAT,实际代码如下所示:

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

原始文件路径已设置为默认的“vncpath1”和“vncpath2”。

temppath 是一个字符串,如果在 "vncpath1" 和 "vncpath2" 上找不到文件,它会获取我们在此处使用此脚本手动设置的新路径

但我的问题是,如果有办法,获取所选 exe 的“原始文件名”并对其进行过滤,所以它只有在(在这种情况下)“vncviewer.exe”时才有效

所以即使我重命名文件,“原始文件名”属性仍然是“vncviewer.exe”

再次感谢 ZET,现在代码的唯一问题是如果 vncviewer.exe 被重命名为 vnc.exe,它将无法工作,这就是为什么我需要获取“原始文件名”属性。

而且因为我喜欢花哨的工作:-)

4

1 回答 1

1

尝试这个:

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
于 2014-10-31T13:06:20.933 回答