如何以编程方式区分Excel 2019和2016?

时间:2019-04-02 23:50:29

标签: excel ms-word ms-office

我维护一个诊断,该诊断以编程方式确定MS Office应用程序的版本,包括Excel和Word。它已针对MS Office版本2003、2007、2010、2013和2016正常运行。但是现在我发现它错误地将MS Office 2019应用程序报告为MS Office 2016。

八年前,哈宁(M. A. Hanin)发表了一个类似的问题:

Identifying Excel version programmatically

mathieu的答复(被接受的答复)与用于在注册表中标识MS Office的产品编号相关联。例如,数字14.0对应于Office2010。DougGlancy用打印出Excel Application对象的version属性的VB代码直接解决了这个问题:

https://docs.microsoft.com/en-us/office/vba/api/excel.application.version

这是一个VB脚本,用于诊断系统上已安装了哪个版本的Excel:

On Error Resume Next
Set excelApp = CreateObject("Excel.Application") 
If Err.Number <> 0 Then
    WScript.Echo "Excel is not installed"
Else
    Wscript.Echo "Excel Version: " & excelApp.Version 
End If

该诊断程序忠实地报告了与2011年以来的帖子一致的MS Office版本。此后,它报告了Office 2013的15.0和Office 2016的16.0。但是,最近,我很惊讶地发现它还报告了Office 2019的16.0那是不对的! 2016年和2019年的功能集明显不同,因此不应将它们组合在一起:

https://support.office.com/en-us/article/what-s-new-in-office-2019-5077cbbe-0d94-44cc-b30e-654e37629b0c?ui=en-US&rs=en-US&ad=US

是否存在另一种以编程方式区分Office 2016与Office 2019的方法?

4 个答案:

答案 0 :(得分:0)

一种解决方法是从使用/ dstatus选项运行的MS Office客户端软件许可证管理工具OSPP.VBS的输出中解析版本号。这是一个示例CMD脚本,演示了解决方法:

:: GetOfficeVer.cmd - Print the version of a licensed MS Office package.
::   Prerequisite:
::      Copy this cmd script to a folder including these MS Office files:
::          * OSPP.VBS    - MS Office Software Protection Platform script.
::          * OSPP.HTM    - Help file for OSPP.VBS.
::          * SLERROR.XML - Data file for OSPP.VBS.
::   Syntax:
::      GetOfficeVer [ComputerName[ PackageAbbr]]
::        ComputerName    - Windows system name  (defaults to local system)
::        PackageAbbr     - Package abbreviation (list below is not exhaustive)
::          *  ProPlus    - Office Professional Plus (default)
::          *  VisioPro   - Visio Professional
::          *  InfoPath   - InfoPath Designer
::   Return Values:
::          *  If the package is licensed, print the MS Office package version
::             string using the MS Office Application.Version property format.
::          *  If the package is unlicensed, print an empty line.
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if %2_==_ (set MSO_PKG=ProPlus) else (set MSO_PKG=%2)
set "MSO_PKG_LIC=cscript "%~dp0\OSPP.VBS" /dstatus %1 | findstr /R /C:"^LICENSE NAME:.*%MSO_PKG%""
for /f "tokens=1-4 delims=, " %%G in ('!MSO_PKG_LIC!') do (set MSO_VER=%%J)
if %MSO_VER%_==_ (echo.) else ( echo %MSO_VER%.0)
endlocal
  

使用OSPP.VBS的提示:

     
      
  • 执行可能需要几秒钟。
  •   
  • 它包含在Application.Path属性中存储的文件夹中。
  •   
  • 不需要将MS Office安装到运行它的系统上。
  •   
  • 为此,它不需要以高架运行。
  •   
  • 如果目标系统需要重新启动,它将失败。
  •   
  • 具有2013年,2016年和2019年版本的版本分别适用于所有三个版本。
  •   

答案 1 :(得分:0)

是的,v16可以是2016或2019

这适用于我的版本。

:GetOfficeVer
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
    >%vbs% @echo off
    >>%vbs% echo Option Explicit ' Enforce variable declaration
    >>%vbs% echo Dim oShell
    >>%vbs% echo Dim lOfficeVersion
    >>%vbs% echo Set oShell = CreateObject("WScript.Shell")
    >>%vbs% echo On Error Resume Next
    >>%vbs% echo lOfficeVersion = GetOfficeVersionNumber() 
    >>%vbs% echo MsgBox "OfficeVersion = " ^& lOfficeVersion
    >>%vbs% echo     Function GetOfficeVersionNumber()
    >>%vbs% echo         GetOfficeVersionNumber = "" 
    >>%vbs% echo         Dim sTempValue
    >>%vbs% echo         sTempValue = oShell.RegRead("HKCR\Excel.Application\CurVer\")
    >>%vbs% echo         If Len(sTempValue) ^> 2 Then GetOfficeVersionNumber = Replace(Right(sTempValue, 2), ^".^", ^"^")
    >>%vbs% echo     End Function   
cscript //nologo %vbs%
pause
if exist %vbs% del /f /q %vbs%
endlocal
goto :EOF

答案 2 :(得分:0)

要扩展已经给出的答案,可悲的是OSPP.VBS是获取我们需要区分Office 2016、2019和365的信息的最明智的方法。

代替执行cscript OSPP.VBS /dstatus,可以通过自己实现OSPP.VBS(在C#中)使用的WMI查询来获取相同的信息:

// Implementation is based on the OSPP.VBS file in the Office directory,
// when it executes the /dstatus command...
// result examples:
// - Office 16, Office16StandardVL_MAK edition
// - Office 19, Office19Standard2019VL_KMS_Client_AE edition
// - Office 16, Office16O365ProPlusR_Subscription1 edition
// - Office 15, OfficeStandardVL_MAK edition

// This constant is apparently the same for all Office versions:
const string officeAppId = "0ff1ce15-a989-479d-af46-f275c6370663";

// Note: OSPP.VBS uses == instead of <= but it seems wrong...
string productClass;
if (Environment.OSVersion.Version <= new Version(6, 1)) // Windows 7 or below
    productClass = "OfficeSoftwareProtectionProduct";
else
    productClass = "SoftwareLicensingProduct";

// Get the product name for all Office products having a product key:
var query = $"SELECT Name FROM {productClass} where "
+ $"ApplicationID=\"{officeAppId}\" AND PartialProductKey <> NULL AND PartialProductKey <> \"\"";

using (var searcher = new System.Management.ManagementObjectSearcher(query))
{
    var result = new List<string>();
    foreach (var instance in searcher.Get())
    {
        result.Add(instance.Properties["Name"].Value?.ToString() ?? "Null");
    }
    
    // result now contains a list of license names with the same format as `cscript OSPP.VBS /dstatus`
}

答案 3 :(得分:-1)

您可以尝试解析excelApp.Path来找出安装路径。 如果在C:* \ Office 19 \ Excel.exe

中安装了新办公室,这将起作用。