Home > Mobile >  Once I determine that Excel is installed on Windows, how can I programmatically tell if it is a lice
Once I determine that Excel is installed on Windows, how can I programmatically tell if it is a lice

Time:11-12

While I was working on my application, my Office 365 subscription expired. The application has an export feature that will export the data into Excel if installed or open it as a formatted text document if not.

Although the subscription is expired, it still opens Excel.

Is there any way to check whether Excel is licensed/registered?

Code used to check for Excel installation:

Dim regKey As Object = My.Computer.Registry.ClassesRoot.OpenSubKey("Excel.Application",  False).OpenSubKey("CurVer", False)
If regKey.GetValue("").ToString() Is Nothing Then
   Return False
Else
   Return True
End If

CodePudding user response:

The following is on Windows 10 box where the location for Office is

C:\Program Files\Microsoft Office\Office16

In short, call cscript.exe using Process.Start in an asynchronous method which writes results to a file and returns the string back to the caller which checks to see if LICENSE STATUS: ---LICENSED--- is in the string, if so the license is good. If more information is needed consider rather than returning a string return a string array read from the file and interrogate other details.

Alternate is to use PowerShell, connect to Azure and then call the appropriate PowerShell command using code similar to below. The following is C# which shows using PowerShell which can easily be converted to VB and follows the same pattern laid out below..

Imports System.IO

Public Class Operations
    Public Shared Async Function OfficeStatusAsync() As Task(Of String)

        Return Await Task.Run(Async Function()
                                  Const fileName = "office.txt"
                                  Const workingFolder = "C:\Program Files\Microsoft Office\Office16"

                                  If Not Directory.Exists(workingFolder) Then
                                      Return "Directory not found"
                                  End If

                                  If File.Exists(fileName) Then
                                      File.Delete(fileName)
                                  End If

                                  Dim start = New ProcessStartInfo With {
                    .FileName = "cscript.exe",
                    .UseShellExecute = False,
                    .RedirectStandardOutput = True,
                    .Arguments = "ospp.vbs /dstatus",
                    .CreateNoWindow = True,
                    .WorkingDirectory = workingFolder
                    }

                                  Using process As Process = Process.Start(start)
                                      Using reader = process.StandardOutput

                                          process.EnableRaisingEvents = True

                                          Dim result = Await reader.ReadToEndAsync()

                                          File.WriteAllText(fileName, result)
                                          process.WaitForExit()

                                          Return File.ReadAllText(fileName)

                                      End Using
                                  End Using

                              End Function)
    End Function

End Class

Usage

Public Class Form1
    Private Async Sub RunButton_Click(sender As Object, e As EventArgs) Handles RunButton.Click
        Dim result = Await Operations.OfficeStatusAsync()
        If result = "Directory not found" Then
            MessageBox.Show("Incorrect folder")
            Return
        End If

        If result.Contains("LICENSE STATUS:  ---LICENSED---") Then
            MessageBox.Show("Good to go")
        Else
            MessageBox.Show("Licence expired")
        End If
    End Sub
End Class
  • Related