vbaautodesk-navisworks

Select which version of program is installed


I have a script that will run depending on which version is installed on a users machine. I have this code but I'm obviously doing something wrong as nothing happens. I think I need to use the Dir function but I'm not exactly sure how.

Sub CheckNavisworksVersion2()
Dim strFileName As String
Dim strNavisworksVersion As String
Dim strNavisworks2020 As String
Dim strNavisworks2021 As String

strNavisworks2020 = "C:\Program Files\Autodesk\Navisworks Manage 2020\FiletoolsTaskRunner.exe"
strNavisworks2021 = "C:\Program Files\Autodesk\Navisworks Manage 2021\FiletoolsTaskRunner.exe"
strFileExists = Dir(strFileName)

If strNavisworksVersion = strNavisworks2020 Then
    MsgBox "Version 2020"
    
ElseIf strNavisworksVersion = strNavisworks2021 Then
    MsgBox "Version 2021"
End If

End Sub

Solution

  • You are not using the right variables in your Dir function. You need to test each one.

    Sub CheckNavisworksVersion2()
    
    Dim strFileExists20 As String
    Dim strNavisworks2020 As String
    strNavisworks2020 = "C:\Program Files\Autodesk\Navisworks Manage 2020\FiletoolsTaskRunner.exe"
    strFileExists20 = Dir(strNavisworks2020)
    If strFileExists20 <> "" Then
        MsgBox "Version 2020 exists"
    End If
    
    Dim strFileExists21 As String
    Dim strNavisworks2021 As String
    strNavisworks2021 = "C:\Program Files\Autodesk\Navisworks Manage 2021\FiletoolsTaskRunner.exe"
    strFileExists21 = Dir(strNavisworks2021)
    If strFileExists21 <> "" Then
        MsgBox "Version 2021 exists"
    End If
    
    End Sub
    

    Or better yet, make it a function:

    Function CheckNavisworksVersion(sVersion As String) As Boolean
      Dim strFileExists As String
      Dim strNavisworks As String
      strNavisworks = "C:\Program Files\Autodesk\Navisworks Manage 20" & sVersion & "\FiletoolsTaskRunner.exe"
      strFileExists = Dir(strNavisworks)
      CheckNavisworksVersion = (strFileExists <> "")
    End Function
    

    Usage:

    Dim bInstalled as Boolean
    bInstalled = CheckNavisworksVersion("20")
    If bInstalled Then MsgBox "Version 2020 exists"
    bInstalled = CheckNavisworksVersion("21")
    If bInstalled Then MsgBox "Version 2021 exists"
    

    Note: Keep in mind that people can install programs to any folder they want to, so this is probably not the best method. Instead, read the registry and look for the keys that exist to determine what is installed.