Detect whether Office is 32bit or 64bit via the registry

前端 未结 28 1103
春和景丽
春和景丽 2020-11-29 21:29

Now that Office also comes in a 64bit install, where in the registry do you find out if the version of Office installed is 32bit or 64bit?

28条回答
  •  旧巷少年郎
    2020-11-29 22:01

    In my tests many of the approaches described here fail, I think because they rely on entries in the Windows registry that turn out to be not reliably present, depending on Office version, how it was installed etc. So a different approach is to not use the registry at all (Ok, so strictly that makes it not an answer to the question as posed), but instead write a script that:

    1. Instantiates Excel
    2. Adds a workbook to that Excel instance
    3. Adds a VBA module to that workbook
    4. Injects a small VBA function that returns the bitness of Office
    5. Calls that function
    6. Cleans up

    Here's that approach implemented in VBScript:

    Function OfficeBitness()
    
        Dim VBACode, Excel, Wb, Module, Result
    
        VBACode = "Function Is64bit() As Boolean" & vbCrLf & _
                  "#If Win64 Then" & vbCrLf & _
                  "    Is64bit = True" & vbCrLf & _
                  "#End If" & vbCrLf & _
                  "End Function"
    
        On Error Resume Next
        Set Excel = CreateObject("Excel.Application")
        Excel.Visible = False
        Set Wb = Excel.Workbooks.Add
        Set Module = Wb.VBProject.VBComponents.Add(1)
        Module.CodeModule.AddFromString VBACode
        Result = Excel.Run("Is64bit")
        Set Module = Nothing
        Wb.Saved = True
        Wb.Close False
        Excel.Quit
        Set Excel = Nothing
        On Error GoTo 0
        If IsEmpty(Result) Then
            OfficeBitness = 0 'Alternatively raise an error here?
        ElseIf Result = True Then
            OfficeBitness = 64
        Else
            OfficeBitness = 32
        End If
    
    End Function
    

    PS. This approach runs more slowly than others here (about 2 seconds on my PC) but it might turn out to be more reliable across different installations and Office versions.

    After some months, I've realised there may be a simpler approach, though still one that instantiates an Excel instance. The VBScript is:

    Function OfficeBitness()
        Dim Excel
        Set Excel = CreateObject("Excel.Application")
        Excel.Visible = False
        If InStr(Excel.OperatingSystem,"64") > 0 Then
            OfficeBitness = 64
        Else
            OfficeBitness = 32
        End if
        Excel.Quit
        Set Excel = Nothing
    End Function
    

    This relies on the fact that Application.OperatingSystem, when called from 32-bit Excel on 64-bit Windows returns Windows (32-bit) NT 10.00 or at least it does on my PC. But that's not mentioned in the docs.

提交回复
热议问题