Solved

Help with checking if excel exist in client's computer

Posted on 2016-10-15
9
61 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 52

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 52

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 52

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 52

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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