How to choose which Excel version to open using VB.NET?
I have several excel sheets which I'd like to work with different Excel versions using VB.NET
Basically, I want to choose which Excel version I will open certain worksheet using VB.NET. Is there any way to know which Excel version is installed in the machine and how many versions with VB.NET? And how do I choose which Excel version I will open it?
I'd like to know it through programming, I know that this can be done if you take a look at the registry keys in the machine. Although, what I need is to create 开发者_如何学运维a software that asks the user for the Excel version he want to open
The following code snippet gets a list of all the installed copies of Excel on the computer (this has been tried and tested on a Windows XP machine, running both Excel 2003 and 2007).
Dim reg As RegistryKey
Dim subKey As RegistryKey
Dim rtn As New Dictionary(Of String, String)
reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Office")
If reg IsNot Nothing Then
For Each subKeyName As String In reg.GetSubKeyNames
subKey = reg.OpenSubKey(subKeyName)
If subKey IsNot Nothing Then
If subKey.GetSubKeyNames().Contains("Excel") Then
subKey = subKey.OpenSubKey("Excel\InstallRoot")
rtn.Add(subKeyName, subKey.GetValue("Path").ToString)
End If
End If
Next
End If
For Each kvp In rtn
MessageBox.Show(String.Format("Version: {0} at '{1}Excel.exe'", kvp.Key, kvp.Value))
Next
In the variable rtn
you have a dictionary of the versions (the key) and the directory that Excel is installed in (the value). As you can see in the MessageBox
bit at the end of my code, you'll need to add "Excel.exe" to the end of it.
Now you have the location of the installed copies of Excel and their versions, you can create a form which lists them, to allow for the user to select which version they wish to use.
Whilst I'm sure you can find the command line for opening an Excel spreadsheet with Excel, I thought I would include it here for completeness;
excel.exe "c:\My Folder\book1.xlsx"
(http://office.microsoft.com/en-us/excel-help/command-line-switches-for-excel-HA010158030.aspx)
A slightly more full explanation of how to identify installed Excel versions and their location.
精彩评论