Solved

Help with checking if excel exist in client's computer

Posted on 2016-10-15
9
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 51

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 51

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
LVL 51

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 70

Expert Comment

by:Éric Moreau
ID: 41844974
0
 
LVL 51

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 34

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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