Search code examples
batch-file64-bit32-bit

batch file code to detect 32-bit vs 64-bit office not working


I'm trying to put together a batch file that would select the appropriate bit version of executables when run by the user. I found this code on stackoverflow: Batch file check office architecture version The problem is that it does not work when I paste it into a text file on my desktop and rename it .bat in my virtual machine that has 64-bit Office 2010 installed: it claims it is 32-bit, the default value set in the code. Not being adept in checking registry entries, I'm wondering if there isn't a ready-made version out there somewhere that actually works. I'm also wondering why there isn't a simple way to determine this. I would have thought that this is something that Microsoft would make easily accessible to developers.


Solution

  • If you're wanting to check the bitness, version and install location of Microsoft Office, you could give this script a test to see if it works for you any better:

    @Echo Off
    If Defined PROCESSOR_ARCHITEW6432 (
        Start "" /D "%__CD__%" "%SystemRoot%\SysNative\cmd.exe" /C "%~f0"&Exit /B)
    Set /A "OSA=MWB=%PROCESSOR_ARCHITECTURE:~-2%"
    If %OSA%==86 Set "MWB=32"
    Set "MOB="&Set "GUID="&Set "REG=%__AppDir__%reg.exe"
    Call :Chk
    If Not Defined MOB If %MWB%==64 Call :Chk \Wow6432Node
    If Not Defined MOB (Echo Microsoft Office product not installed&GoTo :EndIt
    )Else If Not Defined IOV (Echo Unable to determine Microsoft Office version
        GoTo :EndIt)
    Echo %MWB%-bit OS with an Office 20%IOV% %MOB%-bit product installed in %OIL%
    :EndIt
    >Nul Timeout -1
    GoTo :EOF
    
    :Chk
    Set "MOB="&Set "GUID="&Set "OIL="
    Set "Key=HKLM\Software%1\Microsoft\Windows\CurrentVersion\Uninstall"
    For /F Delims^= %%A In ('""%REG%" Query "%Key%" /K /F "*-001B-*0FF1CE}""'
    )Do Set "GUID=%%~nxA"&GoTo :Next
    :Next
    If Not "%GUID:~-1%"=="}" Set "GUID="
    If Not Defined GUID Exit /B
    Set "MOB=32"&If "%GUID:~20,1%"=="1" Set "MOB=64"
    If "%GUID:~4,1%"=="2" Set "IOV=07"
    If "%GUID:~4,1%"=="4" Set "IOV=10"
    If "%GUID:~4,1%"=="5" Set "IOV=13"
    If "%GUID:~4,1%"=="6" Set "IOV=16"
    If Not Defined IOV Exit /B
    For /F Tokens^=2* %%A In ('""%REG%" Query "%Key%\%GUID%" /V "InstallLocation""'
    )Do Set "OIL=%%B"
    Exit /B
    

    Please note however, that this script was designed to work with Windows Microsoft Office products from versions 2007 up to 2016 inclusive. The script only checks for a specific uninstall key for Microsoft Word, (it is assumed that only in very rare cases would somebody install Office without including the Word product), and there is no guarantee that all versions will use the same uninstall key.

    I will not be modifying this code for any additions/changes to your question, or providing help on modifications for purposes other than those for which it was intended.