[英]Using Office Interop Application Object to get the PowerPoint version during installation with Inno Setup
During the installation of our PowerPoint add-in using Inno Setup installer, I need to get the currently used version of PowerPoint by querying an Application.PowerPoint object itself - instead of relying on registry entries which can't be guaranteed to give the correct value .在使用 Inno Setup 安装程序安装我们的 PowerPoint 加载项期间,我需要通过查询 Application.PowerPoint object 本身来获取当前使用的 PowerPoint 版本 - 而不是依赖不能保证提供正确值的注册表项.
I have successfully implemented this for an MSI installer written with WIX based on this answer using this code:我已经使用以下代码基于此答案为使用 WIX 编写的 MSI 安装程序成功实现了此功能:
Imports Microsoft.Office.Interop.PowerPoint
Public Class Environment
Public Shared Function GetPowerPointVersion() As String
Dim CurVer As String
Dim thisPowerPoint As Object
thisPowerPoint = New Application()
CurVer = thisPowerPoint.Version
thisPowerPoint.Quit()
Return CurVer
End Function
End Class
I don't entirely trust this to work in all situations (maybe paranoid), so will put in try/catch blocks and use the registry method if this fails.我不完全相信这在所有情况下都有效(可能是偏执狂),所以如果失败,将放入 try/catch 块并使用注册表方法。
I haven't been able to work out how to do a similar thing with Inno Setup installer.我一直无法弄清楚如何使用 Inno Setup 安装程序做类似的事情。 There are some examples of using DLLs - https://jrsoftware.org/ishelp/index.php?topic=scriptdll - but I can't see how I could create a function callable from Inno Setup from this which would return the version number.有一些使用 DLL 的示例 - https://jrsoftware.org/ishelp/index.php?topic=scriptdll - 但我看不出如何从 Inno Setup 创建一个可调用的 function,它将返回版本号.
You can use CreateOleObject
to call PowerPoint and return the version:您可以使用CreateOleObject
调用 PowerPoint 并返回版本:
[Code]
function GetPowerPointVersion(): string;
var
MyPowerPoint: Variant;
begin
MyPowerPoint := CreateOleObject('PowerPoint.Application');
Result := MyPowerPoint.Version;
MyPowerPoint.Quit;
end;
For completeness, this is the Pascal Script code which I am now using to get the PowerPoint version – based on Matej's answer, with a fallback to checking the registry if that fails:为了完整起见,这是我现在用来获取 PowerPoint 版本的 Pascal 脚本代码——基于 Matej 的回答,如果失败则回退到检查注册表:
function PowerPointVersion(): String;
var
key: String;
versionToUse: String;
installedPowerPoint: Variant;
begin
versionToUse := '';
try
installedPowerPoint := CreateOleObject('PowerPoint.Application');
versionToUse := installedPowerPoint.Version;
installedPowerPoint.Quit;
except
versionToUse := '';
end;
if versionToUse = '' then
begin
if RegQueryStringValue(GetHKLM, 'SOFTWARE\Microsoft\Office\ClickToRun\Configuration','VersionToReport', key) then
begin
versionToUse := key;
Delete(versionToUse, Pos('.', key), Length(key));
versionToUse := versionToUse + '.0';
end;
end;
if versionToUse = '' then
begin
if RegQueryStringValue(HKCR, 'PowerPoint.Application\CurVer\','', key) then
begin
StringChangeEx(key, 'PowerPoint.Application.', '', True);
versionToUse := key;
versionToUse := versionToUse + '.0';
end;
end;
try
// Check to see if versionToUse string can convert to a float:
StrToFloat(versionToUse);
Result := versionToUse;
except
Result := '';
end;
end;
It turns out that using Microsoft.Office.Interop.PowerPoint which is a NuGet package is not a good idea as it is not supported and will be prone to failure.事实证明,使用 Microsoft.Office.Interop.PowerPoint 是一个 NuGet package 不是一个好主意,因为它不受支持并且容易失败。 See this discussion .请参阅此讨论。
This external C# code will work and can be set up to be called from Inno Setup.这个外部 C# 代码可以工作,可以设置为从 Inno Setup 调用。 However, using CreateOleObject within Inno Setup Pascal code as described in the accepted answer is far simpler.但是,如接受的答案中所述,在 Inno Setup Pascal 代码中使用 CreateOleObject 要简单得多。
[SupportedOSPlatform("windows")]
public class PowerPointEnvironment
{
public static string GetPowerPointVersion()
{
string CurVer = "";
Type? PowerPointType = Type.GetTypeFromProgID("PowerPoint.Application");
if (PowerPointType != null)
{
dynamic? thisPowerPoint = Activator.CreateInstance(PowerPointType);
if (thisPowerPoint != null)
{
CurVer = thisPowerPoint.version();
}
}
return CurVer;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.