1

I have a Windows Forms application with 5 methods (each based off of the user clicking a button). In each method, I would like to open the same excel file the same way. However, in each method I want to select a different range on the worksheet. I tried creating a function to open the excel file rather than rewriting it 5 times...

// method to open Excel and load a the workbook based on date selected.
public Tuple<Microsoft.Office.Interop.Excel.Application, Workbook, Worksheet> openExcel() 
{
    Microsoft.Office.Interop.Excel.Application excelObj = new Microsoft.Office.Interop.Excel.Application();    
    string fileName = @"C:\Users\" + userName + @"\Documents\Visual Studio 2015\Projects\ProgramForMom\ProgramForMom\bin\Debug\Excel Files\" + frm2.year.Text + " Expenses"; 
    Workbook wb = excelObj.Workbooks.Open(fileName, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
    wb.Activate();  // Activates file.
    Worksheet ws = wb.Worksheets[frm2.month.Text];   
    ws.Activate();  
    return Tuple.Create(excelObj, wb, ws);
}

All that works fine.

I tried referenced this function in one of the methods...

var excelObj = openExcel();
Workbook wb = openExcel();
Worksheet ws = openExcel();
var cellValue = ws.Range["A1"].Value2;

and I get an error saying...

"Cannot implicitly convert type 'System.Tuple' to 'Microsoft.Office.Interop.Excel.Workbook'. An explicit conversion exists (are you missing a cast?)"

I get the same error for the worksheet. It says the same exact thing just substitutes the word worksheet in place of workbook.

Can you please explain what I have done wrong? Thank you.

2 Answers 2

2
var result = openExcel();
var excelObj = result.Item1;
Workbook wb = result.Item2;
Worksheet ws = result.Item3;
var cellValue = ws.Range["A1"].Value2;
Sign up to request clarification or add additional context in comments.

1 Comment

This also works. So both this solution and the one posted by Mong Zhu are very useful. Just wanted to let people know.
1

You have a mismatch between the return type of your method (which is a Tuple) and the type of the variables in which you want to catch the output of openExcel

it should look more like this

Tuple<Microsoft.Office.Interop.Excel.Application, Workbook, Worksheet> allThreeInOne = openExcell();

then you can try and fiddle everything apart... OR

what you also can do is to access the value right at the point of the function call:

var excelObj = openExcel().Item1;
Workbook wb = openExcel().Item2;
Worksheet ws = openExcel().Item3;

this way you would assign exactly the matching type to the variables

EDIT: Tha latter solution is not advisable since you would unnecessarily open the file 3 times just to get the result that you would have gotten already from the first call,as Joel Coehoorn correctly pointed out.

fiddling the tuple apart would be the way to go:

var excelObj = allThreeInOne.Item1;
Workbook wb = allThreeInOne.Item2;
Worksheet ws = allThreeInOne.Item3;

6 Comments

Perfect! Thank you so much. Exactly what I needed!
you are welcome. Tuples are quite a nice tool to gather information for a short time in one place
Yes definitely. I saw this one post where somebody wanted to create 3 functions or use global variables to achieve something similar to my problem. Your solution is way better! Very easy, neat and time saving.
why don't you actually create one method to load the excel file and pass the different ranges as parameters? this way you could determine with one method which part of the workbook to load and return only the stuff that you need
I have about 7 different ranges. Is it still worth it. If so, could you show how I would do that please?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.