首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可靠地为VBA加载项在Windows注册表中设置密钥

可靠地为VBA加载项在Windows注册表中设置密钥
EN

Code Review用户
提问于 2022-01-04 09:50:33
回答 1查看 52关注 0票数 1

我们有一个PowerPoint VBA,它使用用WIX编写的MSI ()安装。

要让PowerPoint (针对所有用户)获取加载项,需要将名称-值对添加到HKLM:SOFTWARE\Microsoft\Office\[POWERPOINT_VERSION_NUMBER]\PowerPoint\AddIns\OurAddinName中的此注册表项中,其中[POWERPOINT_VERSION_NUMBER]的表单为14.0、16.0等。该加载项适用于PowerPoint 2007及更高版本(即>=12.0)。

为了获得正确的版本号,安装程序一直在查找:PowerPoint.Application\CurVer在香港铁路。这提供了一个类似于PowerPoint.Application.16的值。但是(2022年1月),CurVer的值似乎不可靠。我的一台机器把它命名为PowerPoint.Application.11,而在另一台机器上,它是PowerPoint.Application.16。两者都有相同的最新版本的PowerPoint (版本2111构建16.0.14701.20240)。这两台机器都有单一版本的Office。

如果CurVer给出了不正确的值来计算正确的键来注册加载项,这就产生了以下几个问题:

  • 当用户卸载加载项时,该加载项的路径的注册表项不会被删除(它试图删除HKLM\SOFTWARE\Microsoft\Office\11.0\PowerPoint\AddIns\OurAddinName而不是HKLM\SOFTWARE\Microsoft\Office\16.0\PowerPoint\AddIns\OurAddinName),因此当用户打开PowerPoint时,他们将收到一条关于无法找到加载项的错误消息。
  • 当用户试图安装加载项时,他们会收到一条错误消息,表示他们的PowerPoint版本太老了。

我的工作解决方案

  • 检查HKLM\SOFTWARE\Microsoft\Office\ClickToRun\Configuration\VersionToReport而不是CurVer
  • 检查HKCR\PowerPoint.Application\CurVer是否VersionToReport不可用(可能在旧安装时不可用?)(我们的一些客户组织有很老的办公设备)
  • 将在注册表中找到的版本存储在我们软件的条目下面,这样就可以始终找到卸载的正确位置。

使用WIX和vb.net,如下所示:

获取注册表值:

代码语言:javascript
复制
<Property Id="CTR_POWERPOINT_VERSION" Secure="yes">
  <RegistrySearch Id="RegPowerPointVersion"
                  Root="HKLM"
                  Key="SOFTWARE\Microsoft\Office\ClickToRun\Configuration"
                  Name="VersionToReport"
                  Type="raw">
  </RegistrySearch>
</Property>

<Property Id="CURVER_POWERPOINT_VERSION" Secure="yes">
  <RegistrySearch Id="RegCurVerPowerPointVersion"
                  Root="HKCR"
                  Key="PowerPoint.Application\CurVer"
                  Type="raw">
  </RegistrySearch>
</Property>

<Property Id="RECORDED_POWERPOINT_VERSION" Secure="yes">
  <RegistrySearch Id="RegRecordedPowerPointVersion"
                  Root="HKLM"
                  Key="SOFTWARE\OurCompany\OurAddinName"
                  Name="powerpoint_version"
                  Type="raw">
  </RegistrySearch>
</Property>

可用于自定义操作:

代码语言:javascript
复制
<CustomAction Id="SetPowerPointVersionProperty"
              Property="CTR_POWERPOINT_VERSION"
              HideTarget="no"
              Value="[CTR_POWERPOINT_VERSION]"/>

<CustomAction Id="SetCurVerPowerPointVersionProperty"
              Property="CURVER_POWERPOINT_VERSION"
              HideTarget="no"
              Value="[CURVER_POWERPOINT_VERSION]"/>

<CustomAction Id="SetRecordedPowerPointVersionProperty"
              Property="RECORDED_POWERPOINT_VERSION"
              HideTarget="no"
              Value="[RECORDED_POWERPOINT_VERSION]"/>

使用vb.net处理:

代码语言:javascript
复制
Dim versionToUse As String
versionToUse = Regex.Match(Me.session("RECORDED_POWERPOINT_VERSION"), "[0-9]+").Value

' Note, when updating the addin, this is always done by doing an uninstall
' followed by an install - so in the uninstall phase RECORDED_POWERPOINT_VERSION
' will have something in it, and in the install phase it will be empty.

If versionToUse = vbNullString Then
  versionToUse = Regex.Match(Me.session("CTR_POWERPOINT_VERSION"), "[0-9]+").Value
End If

If versionToUse = vbNullString Then
  versionToUse = Regex.Match(Me.session("CURVER_POWERPOINT_VERSION"), "[0-9]+").Value
End If

Me.session("POWERPOINT_VERSION_NUMBER") = versionToUse & ".0"

设置注册表值(在WOW6432Node上也都重复):

代码语言:javascript
复制
<RegistryKey Root="HKLM"
             Key="SOFTWARE\Microsoft\Office\[POWERPOINT_VERSION_NUMBER]\PowerPoint\AddIns\MyAddinName">
  <RegistryValue Name="AutoLoad"
                 Action="write"
                 Value="1"
                 Type="integer"
                 KeyPath="yes"/>
  
  <RegistryValue Name="Path"
                 Action="write"
                 Value="[APPLICATIONFOLDER]our-addin.ppam"
                 Type="string"
                 KeyPath="no"/>
</RegistryKey>

并将该位置记录在我们的加载项的注册表位置中:

代码语言:javascript
复制
<RegistryKey Root="HKLM"
             Key="SOFTWARE\OurCompany\OurAddinName">

  <RegistryValue Name="powerpoint_version"
                 Action="write"
                 Value="[POWERPOINT_VERSION_NUMBER]"
                 Type="string"
                 KeyPath="no"/>
</RegistryKey>

这个看上去怎么样?

EN

回答 1

Code Review用户

回答已采纳

发布于 2022-01-04 16:39:33

我在VBA中有类似的代码来提取版本。这要简单得多,因为我可以从运行中的应用程序本身提取版本。它知道自己的版本,不关心注册表中存在什么任意值。您可以使用代码从VB.NET中执行此操作。

首先,需要添加一个引用

  1. 在解决方案资源管理器中,右键单击项目名称,然后单击Add Reference。将出现“添加引用”对话框。
  2. 在“程序集”页上,在“组件名称”列表中选择Microsoft.Office.Interop.PowerPoint。如果没有看到程序集,则可能需要确保它们已安装并显示。看看如何:安装Office主互操作程序集
  3. 单击OK。

然后,您需要导入所引用的程序集:

代码语言:javascript
复制
Imports Microsoft.Office.Interop.PowerPoint

然后,您可以按如下方式提取该版本:

代码语言:javascript
复制
Public Function GetPowerPointVersion As String
    Dim CurVer As String
    Using thisPowerPoint As New Microsoft.Office.Interop.PowerPoint.Application()
        CurVer = thisPowerPoint.Version
        thisPowerPoint.Quit
    End Using

    Return CurVer
End Function
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/272609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档