Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

Help with checking if excel exist in client's computer

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
vcharles
Asked:
vcharles
1 Solution
 
Rgonzo1971Commented:
HI,

You could read the registry

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


Regards
0
 
vcharlesAuthor Commented:
Can you please send me the complete code as it would be in the vb.net apllication. Thanks.
0
 
Rgonzo1971Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
vcharlesAuthor Commented:
Hi,

I received the following error message:

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

Victor
0
 
Rgonzo1971Commented:
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
 
vcharlesAuthor Commented:
Hi,

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

Victor
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Rgonzo1971Commented:
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
 
it_saigeDeveloperCommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now