2017-04-03 84 views
1

我需要檢查一個chrome.exe文件是否存在,如果是,打開瀏覽器。我越來越

運行時錯誤 '52' 壞文件名稱或編號

在第一If檢查。這是爲什麼?

Sub openChrome() 
    Dim chromePath32 
    Dim chromePath64 
    Dim chromePathUser 
    Dim current_user 

    chromePath32 = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""" 
    chromePath64 = """C:\Program Files\Google\Chrome\Application\chrome.exe""" 
    current_user = (Environ$("Username")) 
    chromePathUser = """c:\users\" & current_user & "\AppData\Local\Google\Chrome\Application\Chrome.exe""" 

    If Dir$(chromePath32) <> "" Then '<-----error here 
     Shell (chromePath32 & " -url http:google.co.nz") 
    ElseIf Dir$(chromePath64) <> "" Then 
     Shell (chromePath64 & " -url http:google.co.nz") 
    ElseIf Dir$(chromePathUser) <> "" Then 
     Shell (chromePathUser & " -url http:google.co.nz") 
    Else 
     MsgBox "Chrome installation not found" 
     Exit Sub 
    End If 
End Sub 

回答

3

周圍的目錄中刪除您的報價 - 他們沒有必要在Dir命令:

chromePath32 = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 
chromePath64 = "C:\Program Files\Google\Chrome\Application\chrome.exe" 
current_user = (Environ$("Username")) 
chromePathUser = "c:\users\" & current_user & "\AppData\Local\Google\Chrome\Application\Chrome.exe" 

可能需要他們在Shell命令,但我只是想在32位版本和它沒有他們的工作:

If Dir$(chromePath32) <> "" Then 
    Shell """" & chromePath32 & """ -url http:google.co.nz" 
ElseIf Dir$(chromePath64) <> "" Then 
    Shell """" & chromePath64 & """ -url http:google.co.nz" 
ElseIf Dir$(chromePathUser) <> "" Then 
    Shell """" & chromePathUser & """ -url http:google.co.nz" 
Else 
    MsgBox "Chrome installation not found" 
    Exit Sub 
End If