2014-10-17 82 views
1

我正在處理Excel VBA中的代碼,用戶可以在其中選擇默認路徑的文件,如果它不在其默認路徑。excel vba文件過濾爲「原始文件名」

我想過濾它,以確保它們不會選擇錯誤的文件。

我的想法是以某種方式過濾它,以檢查「原始文件名」(您可以在屬性 - >細節中看到)是否與我給出的相同。這樣,即使特定文件被重命名,它也可以工作。

我的問題是,我不知道如何引用它。

編輯

由於昭通,實際的代碼如下所示:

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,它不會工作,這就是爲什麼我需要獲得「原始文件名」屬性。

而且因爲我喜歡:-)

+0

什麼是原始文件名:title屬性或工作簿的名稱?什麼是path1和path2以及temppath?您的文件擴展名是.exe? – ZAT 2014-10-18 13:52:01

+0

是嗎?這甚至有可能嗎? – Divin3 2014-10-30 22:43:20

回答

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 
+0

使用了該空間,因爲該exe文件是以附加信息打開的。例如「C:\ Program Files \ RealVNC \ VNC Viewer \ vncviewer.exe 172.21.1.1」這將使vnc連接到給定的ip地址。 我會盡快嘗試!謝謝! – Divin3 2014-11-04 14:43:42

+0

@ Divin3如果目的服務upvote和/或標記爲答案關閉它。 – ZAT 2014-11-05 07:22:46

+0

我給你upvote,因爲這解決了大部分問題,但它仍然只檢查文件名,而不是「原始文件名」。這意味着如果我將文件重命名爲「vncviewerrrrr.exe」,它將不起作用。這就是爲什麼我問是否有辦法檢查「原始文件名」屬性。感謝您的幫助 – Divin3 2014-11-05 16:18:52

相關問題