Solved

Help with checking if excel exist in client's computer

Posted on 2016-10-15
9
36 Views
Last Modified: 2016-11-25
Hi.

How do you find out if excel exist and which version of excel is on a client's computer using VB.NET?

Thanks,

Victor
0
Comment
Question by:vcharles
9 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41844718
HI,

You could read the registry

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe
and read the path


Regards
0
 

Author Comment

by:vcharles
ID: 41844728
Can you please send me the complete code as it would be in the vb.net apllication. Thanks.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41844729
try something like this

Dim readValue = My.Computer.Registry.GetValue(
    "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", "Path", Nothing)
MsgBox("The value is " & readValue)
0
 

Author Comment

by:vcharles
ID: 41844733
Hi,

I received the following error message:

Registry key name must start with a valid base key name.

Victor
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41844736
then try

Dim readValue = My.Computer.Registry.GetValue(
     "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", "Path", Nothing)
 MsgBox("The value is " & readValue)
0
 

Author Comment

by:vcharles
ID: 41844747
Hi,

The message box shows readValue is empty, but I do have excel on my PC.

Victor
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41844974
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41846162
Has your code

Imports Microsoft.Win32

 to read the registry
Dim readValue As String = _
            Registry.GetValue( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", "Path", "")
Console.WriteLine( readValue )

Open in new window

0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41846681
Another couple of methods (although one could potentially be what is used and recommended by Eric):
Imports Microsoft.Office.Interop
Imports Microsoft.Win32

Module Module1
	Sub Main()
		Console.Write("Detect using COM object - ")
		DetectFromCOMObject()
		Console.Write("Detect using Registry - ")
		DetectFromRegistry()
		Console.ReadLine()
	End Sub

	Private Sub DetectFromCOMObject()
		Dim objEApp As Excel.Application = Nothing
		Try
			objEApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
			Select Case objEApp.Version
				Case "7.0"
					Console.WriteLine("Excel 95")
				Case "8.0"
					Console.WriteLine("Excel 97")
				Case "9.0"
					Console.WriteLine("Excel 2000")
				Case "10.0"
					Console.WriteLine("Excel 2002")
				Case "11.0"
					Console.WriteLine("Excel 2003")
				Case "12.0"
					Console.WriteLine("Excel 2007")
				Case "14.0"
					Console.WriteLine("Excel 2010")
				Case "15.0"
					Console.WriteLine("Excel 2013")
				Case "16.0"
					Console.WriteLine("Excel 2016")
				Case Else
					Console.WriteLine("Undocumented Version - {0}", objEApp.Version)
			End Select
		Catch ex As Exception
			Console.WriteLine("Excel is not installed or an instance cannot be created")
		Finally
			If objEApp IsNot Nothing Then objEApp.Quit()
			objEApp = Nothing
		End Try
	End Sub

	Private Sub DetectFromRegistry()
		Using key As RegistryKey = Registry.ClassesRoot.OpenSubKey("\Excel.Application\CurVer", False)
			If key IsNot Nothing Then
				Dim value As String = key.GetValue(String.Empty)
				value = value.Substring(value.LastIndexOf(".") + 1)
				Select Case value
					Case "7"
						Console.WriteLine("Excel 95")
					Case "8"
						Console.WriteLine("Excel 97")
					Case "9"
						Console.WriteLine("Excel 2000")
					Case "10"
						Console.WriteLine("Excel 2002")
					Case "11"
						Console.WriteLine("Excel 2003")
					Case "12"
						Console.WriteLine("Excel 2007")
					Case "14"
						Console.WriteLine("Excel 2010")
					Case "15"
						Console.WriteLine("Excel 2013")
					Case "16"
						Console.WriteLine("Excel 2016")
					Case Else
						Console.WriteLine("Undocumented Version - {0}", value)
				End Select
			End If
		End Using
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPGFor the COM object method, don't forget to add a reference to the Microsoft Excel Object Library.  You will want to ensure that you add a reference for the oldest version that you want to support and then set the Embed Interop Types property for the reference to True (http://www.claudiobernasconi.ch/2014/02/13/painless-office-interop-using-visual-c-sharp/).  COM object support is more restrictive as you are limited based upon the oldest PIA that can be found (I believe XP/2002 is the oldest supported but I could be wrong).

-saige-
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now