运行时错误' 52' Dir $函数的文件名或编号错误

时间:2017-04-03 20:33:04

标签: excel vba excel-vba

我需要检查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

1 个答案:

答案 0 :(得分: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