Home > Net >  Using Office Interop Application Object to get the PowerPoint version during installation with Inno
Using Office Interop Application Object to get the PowerPoint version during installation with Inno

Time:01-26

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.

I have successfully implemented this for an MSI installer written with WIX based on this answer using this code:

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.

I haven't been able to work out how to do a similar thing with Inno Setup installer. 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.

UPDATE - 25 January 2023

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. See this discussion.

This external C# code will work and can be set up to be called from Inno Setup. However, using CreateOleObject as described in the accepted answer is far simpler.

[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;
    }
}

CodePudding user response:

You can use CreateOleObject to call PowerPoint and return the version:

[Code]
function GetPowerPointVersion(): string;
var
  MyPowerPoint: Variant;
begin
  MyPowerPoint := CreateOleObject('PowerPoint.Application');
  Result := MyPowerPoint.Version;
  MyPowerPoint.Quit;
end;

CodePudding user response:

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:

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;
  • Related