Results 1 to 5 of 5

Thread: VB - How to tell if an Office component is installed

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    VB - How to tell if an Office component is installed

    If you want to know whether or not a particular Office component is available before attempting automation...

    VB Code:
    1. Private Enum OfficeComponents
    2.     Word = 0
    3.     Excel = 1
    4.     PowerPoint = 2
    5.     Access = 3
    6. End Enum
    7.  
    8. Private Function IsOfficeComponentInstalled(Component As OfficeComponents) As Boolean
    9. Dim s As String
    10. Dim o As Object
    11.  
    12.     Select Case Component
    13.         Case Word:       s = "Word"
    14.         Case Excel:      s = "Excel"
    15.         Case PowerPoint: s = "PowerPoint"
    16.         Case Access:     s = "Access"
    17.     End Select
    18.    
    19. On Error Resume Next
    20.     Set o = CreateObject(s & ".Application")
    21.     IsOfficeComponentInstalled = Not (o Is Nothing)
    22.    
    23.     Set o = Nothing
    24.    
    25. End Function
    26.  
    27. Private Sub Command1_Click()
    28.  
    29.     MsgBox IsOfficeComponentInstalled(Word)
    30.     MsgBox IsOfficeComponentInstalled(Excel)
    31.     MsgBox IsOfficeComponentInstalled(PowerPoint)
    32.     MsgBox IsOfficeComponentInstalled(Access)
    33.  
    34. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69
    I've written a little Installation Test-Program, to check a customer PC for installed components.

    I check several dlls, activex-controls and office components and the last part is nearly the same as your tip. Except two lines

    VB Code:
    1. On Error Resume Next
    2.     Set o = CreateObject(s & ".Application")
    3.     IsOfficeComponentInstalled = Not (o Is Nothing)
    4.    
    5.     o.quit false
    6.     o.quit
    7.    
    8.     Set o = Nothing

    some apps may hang if you don't terminate them the way they want!
    Btw.: this works with visio and MapPoint, too!

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by blaff

    VB Code:
    1. '...
    2.     o.quit false
    3.     o.quit
    4.     '...
    Excellent. I would never have guessed that.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69

    Check the installed version

    If you want to check the installed version (build from sveral code snippets all over the web):



    VB Code:
    1. Private Function IsOfficeComponentInstalled(Component As OfficeComponents, byref blnCreated as Boolean) As String
    2. Dim s As String
    3. Dim o As Object
    4. sim strFname as String
    5.  
    6.     blnCreated = False
    7.    
    8.     Select Case Component
    9.         Case Word:       s = "Word"
    10.         Case Excel:      s = "Excel"
    11.         Case PowerPoint: s = "PowerPoint"
    12.         Case Access:     s = "Access"
    13.     End Select
    14.    
    15.     strFname =  GetFileFromProgID(s)
    16.     IsOfficeComponentInstalled=FileVerInfo(strFname)
    17.  
    18. On Error Resume Next
    19.     Set o = CreateObject(s & ".Application")
    20.     blnCreated = Not (o Is Nothing)
    21.    
    22.     Set o = Nothing
    23.     o.quit false
    24.     o.quit
    25.    
    26. End Function
    with one module for Version Infos:
    VB Code:
    1. Option Explicit
    2. Type VS_FIXEDFILEINFO
    3.  
    4.    dwSignature As Long
    5.    dwStrucVersionl As Integer     '  e.g. = &h0000 = 0
    6.    dwStrucVersionh As Integer     '  e.g. = &h0042 = .42
    7.    dwFileVersionMSl As Integer    '  e.g. = &h0003 = 3
    8.    dwFileVersionMSh As Integer    '  e.g. = &h0075 = .75
    9.    dwFileVersionLSl As Integer    '  e.g. = &h0000 = 0
    10.    dwFileVersionLSh As Integer    '  e.g. = &h0031 = .31
    11.    dwProductVersionMSl As Integer '  e.g. = &h0003 = 3
    12.    dwProductVersionMSh As Integer '  e.g. = &h0010 = .1
    13.    dwProductVersionLSl As Integer '  e.g. = &h0000 = 0
    14.    dwProductVersionLSh As Integer '  e.g. = &h0031 = .31
    15.    dwFileFlagsMask As Long        '  = &h3F for version "0.42"
    16.    dwFileFlags As Long            '  e.g. VFF_DEBUG Or VFF_PRERELEASE
    17.    dwFileOS As Long               '  e.g. VOS_DOS_WINDOWS16
    18.    dwFileType As Long             '  e.g. VFT_DRIVER
    19.    dwFileSubtype As Long          '  e.g. VFT2_DRV_KEYBOARD
    20.    dwFileDateMS As Long           '  e.g. 0
    21.    dwFileDateLS As Long           '  e.g. 0
    22. End Type
    23.  
    24. Declare Function GetFileVersionInfo Lib "version.dll" Alias _
    25.    "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal _
    26.    dwHandle As Long, ByVal dwLen As Long, lpData As Any) As Long
    27. Declare Function GetFileVersionInfoSize Lib "version.dll" Alias _
    28.    "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, _
    29.    lpdwHandle As Long) As Long
    30. Declare Function VerQueryValue Lib "version.dll" Alias _
    31.    "VerQueryValueA" (pBlock As Any, ByVal lpSubBlock As String, _
    32.    lplpBuffer As Any, puLen As Long) As Long
    33. Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
    34.    (dest As Any, ByVal source As Long, ByVal length As Long)
    35. Declare Function GetSystemDirectory Lib "kernel32" Alias _
    36.    "GetSystemDirectoryA" _
    37.    (ByVal path As String, ByVal cbBytes As Long) As Long
    38.  
    39. ' ===== From Win32 Ver.h =================
    40. ' ----- VS_VERSION.dwFileFlags -----
    41. Public Const VS_FFI_SIGNATURE = &HFEEF04BD
    42. Public Const VS_FFI_STRUCVERSION = &H10000
    43. Public Const VS_FFI_FILEFLAGSMASK = &H3F&
    44.  
    45. ' ----- VS_VERSION.dwFileFlags -----
    46. Public Const VS_FF_DEBUG = &H1
    47. Public Const VS_FF_PRERELEASE = &H2
    48. Public Const VS_FF_PATCHED = &H4
    49. Public Const VS_FF_PRIVATEBUILD = &H8
    50. Public Const VS_FF_INFOINFERRED = &H10
    51. Public Const VS_FF_SPECIALBUILD = &H20
    52.  
    53. ' ----- VS_VERSION.dwFileOS -----
    54. Public Const VOS_UNKNOWN = &H0
    55. Public Const VOS_DOS = &H10000
    56. Public Const VOS_OS216 = &H20000
    57. Public Const VOS_OS232 = &H30000
    58. Public Const VOS_NT = &H40000
    59. Public Const VOS__BASE = &H0
    60. Public Const VOS__WINDOWS16 = &H1
    61. Public Const VOS__PM16 = &H2
    62. Public Const VOS__PM32 = &H3
    63. Public Const VOS__WINDOWS32 = &H4
    64.  
    65. Public Const VOS_DOS_WINDOWS16 = &H10001
    66. Public Const VOS_DOS_WINDOWS32 = &H10004
    67. Public Const VOS_OS216_PM16 = &H20002
    68. Public Const VOS_OS232_PM32 = &H30003
    69. Public Const VOS_NT_WINDOWS32 = &H40004
    70.  
    71.  
    72. ' ----- VS_VERSION.dwFileType -----
    73. Public Const VFT_UNKNOWN = &H0
    74. Public Const VFT_APP = &H1
    75. Public Const VFT_DLL = &H2
    76. Public Const VFT_DRV = &H3
    77. Public Const VFT_FONT = &H4
    78. Public Const VFT_VXD = &H5
    79. Public Const VFT_STATIC_LIB = &H7
    80.  
    81. ' ----- VS_VERSION.dwFileSubtype for VFT_WINDOWS_DRV -----
    82. Public Const VFT2_UNKNOWN = &H0
    83. Public Const VFT2_DRV_PRINTER = &H1
    84. Public Const VFT2_DRV_KEYBOARD = &H2
    85. Public Const VFT2_DRV_LANGUAGE = &H3
    86. Public Const VFT2_DRV_DISPLAY = &H4
    87. Public Const VFT2_DRV_MOUSE = &H5
    88. Public Const VFT2_DRV_NETWORK = &H6
    89. Public Const VFT2_DRV_SYSTEM = &H7
    90. Public Const VFT2_DRV_INSTALLABLE = &H8
    91. Public Const VFT2_DRV_SOUND = &H9
    92. Public Const VFT2_DRV_COMM = &HA
    93.  
    94. Private Const dhcMaxPath = 260
    95.  
    96. Public Function tt_FileVerInfo(FullFileName As String) As String
    97.    Dim strMessage As String
    98.    Dim rc                As Long
    99.    Dim lDummy            As Long
    100.    Dim sBuffer()         As Byte
    101.    Dim lBufferLen        As Long
    102.    Dim lVerPointer       As Long
    103.    Dim udtVerBuffer      As VS_FIXEDFILEINFO
    104.    Dim lVerbufferLen     As Long
    105.    
    106.    '*** Get size ****
    107.    lBufferLen = GetFileVersionInfoSize(FullFileName, lDummy)
    108.    If lBufferLen < 1 Then
    109.     tt_FileVerInfo = "Not available"
    110.     Exit Function
    111.    End If
    112.    
    113.    '**** Store info to udtVerBuffer struct ****
    114.    ReDim sBuffer(lBufferLen)
    115.    rc = GetFileVersionInfo(FullFileName, 0&, lBufferLen, sBuffer(0))
    116.    rc = VerQueryValue(sBuffer(0), "\", lVerPointer, lVerbufferLen)
    117.    MoveMemory udtVerBuffer, lVerPointer, Len(udtVerBuffer)
    118.    
    119.    '**** Determine Product Version number ****
    120. '   tt_ProdVerInfo = Format$(udtVerBuffer.dwProductVersionMSh) & "." & _
    121. '      Format$(udtVerBuffer.dwProductVersionMSl) & "." & _
    122. '      Format$(udtVerBuffer.dwProductVersionLSh) & "." & _
    123. '      Format$(udtVerBuffer.dwProductVersionLSl)
    124.    tt_FileVerInfo = Format$(udtVerBuffer.dwFileVersionMSh) & "." & _
    125.       Format$(udtVerBuffer.dwFileVersionMSl) & "." & _
    126.       Format$(udtVerBuffer.dwFileVersionLSh) & "." & _
    127.       Format$(udtVerBuffer.dwFileVersionLSl)
    128.  
    129. End Function
    130.  
    131.  
    132. Function tt_SystemDirectory() As String
    133.     ' Retrieve the system directory.
    134.     Dim strBuffer As String
    135.     Dim lngLen As Long
    136.    
    137.     strBuffer = Space(dhcMaxPath)
    138.     lngLen = dhcMaxPath
    139.    
    140.     lngLen = GetSystemDirectory(strBuffer, lngLen)
    141.     ' If the path is longer than dhcMaxPath, then
    142.     ' lngLen contains the correct length. Resize the
    143.     ' buffer and try again.
    144.     If lngLen > dhcMaxPath Then
    145.         strBuffer = Space(lngLen)
    146.         lngLen = GetSystemDirectory(strBuffer, lngLen)
    147.     End If
    148.    
    149.     tt_SystemDirectory = Left$(strBuffer, lngLen)
    150.  
    151. End Function
    152.  
    153. Function GetFileFromProgID(ByVal ProgID As String) As String
    154.     Dim clsid As String
    155.     Const HKEY_CLASSES_ROOT = &H80000000
    156.  
    157.     ' get the CLSID from the registry, exit if not found
    158.     clsid = GetRegistryValue(HKEY_CLASSES_ROOT, ProgID & "\CLSID", "")
    159.     If Len(clsid) = 0 Then Exit Function
    160.    
    161.     ' try to read the HKEY_CLASSES_ROOT\CLSID\{...}\InProcServer32 value
    162.     GetFileFromProgID = GetRegistryValue(HKEY_CLASSES_ROOT, _
    163.         "CLSID\" & clsid & "\InProcServer32", "")
    164.     ' exit if found
    165.     If Len(GetFileFromProgID) Then Exit Function
    166.    
    167.     ' try to read the HKEY_CLASSES_ROOT\CLSID\{...}\LocalServer32 value
    168.     GetFileFromProgID = GetRegistryValue(HKEY_CLASSES_ROOT, _
    169.         "CLSID\" & clsid & "\LocalServer32", "")
    170.    
    171. End Function

    and one Module for the Registry Access

    following in the next message

  5. #5
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69

    Registry Access

    Voilà:

    VB Code:
    1. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    2.     (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    3.     ByVal samDesired As Long, phkResult As Long) As Long
    4. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
    5.     Long
    6. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
    7.     "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
    8.     ByVal lpReserved As Long, lpType As Long, lpData As Any, _
    9.     lpcbData As Long) As Long
    10. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    11.     Any, source As Any, ByVal numBytes As Long)
    12.  
    13. Const KEY_READ = &H20019  ' ((READ_CONTROL Or KEY_QUERY_VALUE Or
    14.                           ' KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not
    15.                           ' SYNCHRONIZE))
    16.  
    17. Const REG_SZ = 1
    18. Const REG_EXPAND_SZ = 2
    19. Const REG_BINARY = 3
    20. Const REG_DWORD = 4
    21. Const REG_MULTI_SZ = 7
    22. Const ERROR_MORE_DATA = 234
    23.  
    24. ' Read a Registry value
    25. '
    26. ' Use KeyName = "" for the default value
    27. ' If the value isn't there, it returns the DefaultValue
    28. ' argument, or Empty if the argument has been omitted
    29. '
    30. ' Supports DWORD, REG_SZ, REG_EXPAND_SZ, REG_BINARY and REG_MULTI_SZ
    31. ' REG_MULTI_SZ values are returned as a null-delimited stream of strings
    32. ' (VB6 users can use SPlit to convert to an array of string)
    33.  
    34. Function GetRegistryValue(ByVal hKey As Long, ByVal KeyName As String, _
    35.     ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
    36.     Dim handle As Long
    37.     Dim resLong As Long
    38.     Dim resString As String
    39.     Dim resBinary() As Byte
    40.     Dim length As Long
    41.     Dim retVal As Long
    42.     Dim valueType As Long
    43.    
    44.     ' Prepare the default result
    45.     GetRegistryValue = IIf(IsMissing(DefaultValue), Empty, DefaultValue)
    46.    
    47.     ' Open the key, exit if not found.
    48.     If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) Then
    49.         Exit Function
    50.     End If
    51.    
    52.     ' prepare a 1K receiving resBinary
    53.     length = 1024
    54.     ReDim resBinary(0 To length - 1) As Byte
    55.    
    56.     ' read the registry key
    57.     retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _
    58.         length)
    59.     ' if resBinary was too small, try again
    60.     If retVal = ERROR_MORE_DATA Then
    61.         ' enlarge the resBinary, and read the value again
    62.         ReDim resBinary(0 To length - 1) As Byte
    63.         retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _
    64.             length)
    65.     End If
    66.    
    67.     ' return a value corresponding to the value type
    68.     Select Case valueType
    69.         Case REG_DWORD
    70.             CopyMemory resLong, resBinary(0), 4
    71.             GetRegistryValue = resLong
    72.         Case REG_SZ, REG_EXPAND_SZ
    73.             ' copy everything but the trailing null char
    74.             resString = Space$(length - 1)
    75.             CopyMemory ByVal resString, resBinary(0), length - 1
    76.             GetRegistryValue = resString
    77.         Case REG_BINARY
    78.             ' resize the result resBinary
    79.             If length <> UBound(resBinary) + 1 Then
    80.                 ReDim Preserve resBinary(0 To length - 1) As Byte
    81.             End If
    82.             GetRegistryValue = resBinary()
    83.         Case REG_MULTI_SZ
    84.             ' copy everything but the 2 trailing null chars
    85.             resString = Space$(length - 2)
    86.             CopyMemory ByVal resString, resBinary(0), length - 2
    87.             GetRegistryValue = resString
    88.         Case Else
    89.             RegCloseKey handle
    90.             Err.Raise 1001, , "Unsupported value type"
    91.     End Select
    92.    
    93.     ' close the registry key
    94.     RegCloseKey handle
    95. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width