I have code below which determines the version of Access. It runs quickly on most PCs. We also have four terminal servers. On two of the terminal servers it runs fine. On th
This is how to obtain the installed Excel version.
Imports Microsoft.Office.Interop.Excel
Public Class ExcelVersion
Dim xl As Application
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handlers NextCmd.Click
xl = New Application
MsgBox xl.Version()
xl.Quit()
End Sub
End Class
I think the problem is the call to CreateObject(). This will run up Access which I guess can take 15 seconds on some machines. Here’s a alternative way to get the version number which should be a lot faster – it uses the information in the registery.
Imports Microsoft.Win32
Public Class AccessInterop
Public Shared Function GetAccessVersionNiceName() As String
Try
Dim ClassName As String = GetAccessClassName()
Select Case GetAccessVersionNumber(ClassName)
Case 8
Return "Access 97"
Case 9
Return "Access 2000"
Case 10
Return "Access XP"
Case 11
Return "Access 2003"
Case 12
Return "Access 2007"
Case 13
Return "Access 2010"
Case Else
Return "unknown"
End Select
Catch ex As Exception
Return "unknown"
End Try
End Function
Private Shared Function GetAccessClassName() As String
Dim RegKey As RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("Access.Application\CurVer")
If RegKey Is Nothing Then
Throw New ApplicationException("Can not find MS Access version number in registry")
Else
Return RegKey.GetValue("")
End If
End Function
Public Shared Function GetAccessVersionNumber(ByVal ClassName As String) As Integer
Dim VersionNumber As String = ClassName
While VersionNumber.IndexOf(".") > -1
VersionNumber = VersionNumber.Substring(VersionNumber.IndexOf(".") + 1)
End While
Return VersionNumber.Trim
End Function
End Class
This example returns a list of installed versions of Access quite quickly. There is no need to check further if only one is returned.
Const HKEY_LOCAL_MACHINE = &H80000002&
Set fs = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\default:StdRegProv")
strKeyPathOrg = "SOFTWARE\Microsoft\Office"
strKeyPath = strKeyPathOrg
strValueName = "Path"
strKeyPath = strKeyPathOrg
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
Select Case subkey
Case "14.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access 2010" & vbCrLf
End If
End If
Case "12.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access 2007" & vbCrLf
End If
End If
Case "11.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access 2003" & vbCrLf
End If
End If
Case "10.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access XP" & vbCrLf
End If
End If
Case "9.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access 2000" & vbCrLf
End If
End If
Case "8.0"
strKeyPath = strKeyPathOrg & "\" & subkey & "\Access\InstallRoot\"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If Not IsNull(strValue) Then
If fs.FileExists(strValue & "msaccess.exe") Then
r = r & "Has Access 97" & vbCrLf
End If
End If
End Select
Next
MsgBox r
This is a VERY long shot, but if you're running a compiled .NET app, make sure the machines you're running the app on have Internet access, because the .NET apps like to connect to Microsoft's website to validate.