I'm trying to automate excel from visual c#. One thing I need to be able to
test is if excel is even installed on the target machine and what version of
excel is installed on the target machine. Is there a way to do this via c#
code?
Thanks.
Mansi
¤
¤ I'm trying to automate excel from visual c#. One thing I need to be able to
¤ test is if excel is even installed on the target machine and what version of
¤ excel is installed on the target machine. Is there a way to do this via c#
¤ code?
The following code should work but it depends upon correct registry entries for Excel. It's in
VB.NET but shouldn't be difficult to convert:
Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As
String, _
ByVal lpDirectory As
String, _
ByVal lpResult As
System.Text.StringBuilder) As Int32
Function IsExcelInstalled(ByRef FileVersionMajor As String) As Boolean
Dim DummyFile As String
Dim FileDir As String
'Const MAX_PATH As Integer = 255
Dim FilePath As New System.Text.StringBuilder(255)
DummyFile = "e:\My Documents\book1.xls"
If FindExecutable(DummyFile, FileDir, FilePath) > 32 Then
IsExcelInstalled = True
FileVersionMajor =
System.Diagnostics.FileVersionInfo.GetVersionInfo(FilePath.ToString).FileMajorPart
End If
End Function
The alternative is to use Excel automation and catch any runtime errors.
Function IsExcelInstalledAuto(ByRef FileVersion As String) As Boolean
Dim ExcelObject As Object
Try
ExcelObject = CreateObject("Excel.Application")
FileVersion = ExcelObject.Version
ExcelObject.Quit()
Return True
Catch ex As Exception
If ex.Message = "Cannot create ActiveX component." Then
Return False
Else
Throw
End If
End Try
End Function
Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Mansi - 22 Jan 2005 20:41 GMT
Thanks Paul. The second method you suggested works great!
Thanks for the help.
Mansi
> ¤
> ¤ I'm trying to automate excel from visual c#. One thing I need to be able to
[quoted text clipped - 52 lines]
> Paul ~~~ pclement@ameritech.net
> Microsoft MVP (Visual Basic)