Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

How to test if Microsoft Excel is installed?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mansi - 21 Jan 2005 05:09 GMT
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
Paul Clement - 21 Jan 2005 17:48 GMT
¤
¤ 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)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.