Search code examples
vba7zip

Check right installation of 7-Zip (64 / 32bit)


My goal is avoid error msg from VBA debugger. Need to check which version of 7-zip have installed, program files/ or program files (x86):

Trying do simple "IF" function.

Dim PathZipProgram As String
strCommand As String

PathZipProgram = "C:\Program Files(x86)\7-Zip\7z.exe"
If Right(PathZipProgram, 1) Then
    PathZipProgram = PathZipProgram
Else
    PathZipProgram = "C:\Program Files\7-Zip\7z.exe"
End If

Shell strCommand

strCommand = """" & PathZipProgram & """ a -tzip """

VBA cant find 7zip.


Solution

  • You can check if the file exist with a function like this:

    Function FileExists(FilePath As String) As Boolean
    Dim TestStr As String
        TestStr = ""
        On Error Resume Next
        TestStr = Dir(FilePath)
        On Error GoTo 0
        If TestStr = "" Then
            FileExists = False
        Else
            FileExists = True
        End If
    End Function
    

    And then use it in your code:

    Dim PathZipProgram As String
    Dim strCommand As String
    
    PathZipProgram = "C:\Program Files(x86)\7-Zip\7z.exe"
    
    If Not FileExists(PathZipProgram) Then
        PathZipProgram = "C:\Program Files\7-Zip\7z.exe"
    End If
    
    Shell strCommand
    
    strCommand = """" & PathZipProgram & """ a -tzip """
    

    Hope this help as a starting point.