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

x
?
Solved

VB.Net wrong Excel version opening

Posted on 2010-08-19
17
Medium Priority
?
390 Views
Last Modified: 2012-05-10
Hi

I have a Windows form in which I want to open a file in Excel 2002.

It must be 2002 that starts, not 2003 (due to a change in the 2003 algorithm for LINEST)

Unfortunately this code starts Excel 2003, not 2002

    xlsApp = New Excel.Application
    xlsApp.Visible = True
    xlsWB = xlsApp.Workbooks.Open(path)
   
The project references are to Excel 10.0 DLL's

Help please in forcing 2002 to open, thank-you in advance
0
Comment
Question by:rwallacej
  • 5
  • 5
  • 3
  • +2
16 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33473515
The only way to do that (assuming you have multiple versions installed) is to Shell the excel.exe for the version you want.
0
 
LVL 3

Expert Comment

by:LDH
ID: 33473570
Try referring to the Excel 9.0 DLL's..
0
 

Author Comment

by:rwallacej
ID: 33473571
ok so how do I do that?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33473581
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33473611
In .Net I don't know. In VB6 you would just use Shell:
Shell """C:\path to msofficeXP\excel.exe"" ""c:\path to workbook\.xls""", vbnormalfocus
and you can then use GetObject with the workbook name to get a reference to that instance of Excel.




0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33473629
My Project -References In .net
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33473646
References won't work. If you have multiple versions, the default one will start regardless unless you run the specific executable.
0
 

Author Comment

by:rwallacej
ID: 33473648
the references are set to 10.0.....this is Excel 2002 from what I have read

tried changing from new Excel.Application to be

   xlsApp = CreateObject("Excel.Application")

but still got 2003 starting
0
 

Author Comment

by:rwallacej
ID: 33473651
the Shell idea sounds best, if I knew how it worked in .Net
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 1000 total points
ID: 33473714
A quick google seems to imply that System.Diagnostics.Process.Start does the same thing (but doesn't take the second parameter for window style). See this page: http://www.devx.com/dotnet/Article/7914
HTH

Rory
0
 
LVL 5

Expert Comment

by:DerZauberer
ID: 33473826
not sure if that might help, but try

xlsApp = CreateObject("Excel.Application.9")

It actually depends on what objects are registered in the windows registry under "HKEY_CLASSES_ROOT\Excel.Application*"

0
 

Author Comment

by:rwallacej
ID: 33475867
trying

xlsApp = CreateObject("Excel.Application.9")

gives error "Cannot create ActiveX component"

.10 starts Excel 2003

under windows registry there is
Excel.Application
Excel.Application.10
Excel.Application.11
0
 

Author Comment

by:rwallacej
ID: 33475964
Try referring to the Excel 9.0 DLL's..
-->I only have 5, 10 & 11 DLLs, not 9
picking 5 means compile error "cannot create new instance of interface"
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33476719
10 is the correct version for 2002. You cannot use CreateObject or New Excel.Application. It will not work.
0
 
LVL 5

Expert Comment

by:DerZauberer
ID: 33509928
In VB.NET the "Shell" function still exists in namespace Microsoft.VisualBasic.Comaptibility

You can use it in a similar way like rorya suggested to launch the correct Excel.exe application.
0
 
LVL 5

Assisted Solution

by:DerZauberer
DerZauberer earned 1000 total points
ID: 33510068
Well there is a more .NET like way to start applications and you might need to access the application-object somehow.

You can go like this:

System.Diagnostics.Process process = System.Diagnostics.Process.Start(@"C:\Program Files\<path to your excel version>\Excel.exe");
int processId = process.Id;

Afterwards use the function AccessibleObjectFromWindow to gain access to the object model.

You can look up the documentation here:

http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

773 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