1

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
JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
BigNom
  • 93
  • 2
  • 8

1 Answers1

0

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.

braX
  • 11,506
  • 5
  • 20
  • 33
  • works like a treat, thankyou very much. Okay thanks, I will do some research on using the registry to look for keys – BigNom Nov 05 '20 at 06:42