Re: C# Excel Workbook Open
First, use [ code][ /code] tags please.
Second, you're declaring the 'excelWorkbook' variable inside of a method. Therefore, it has no scope outside of the method.
In your Constructor:
Code:
Workbook excelWorkbook = excelApplication.Workbooks.Open(exlpath, 3, false, 5, "", ""
, true, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
In your button1_Click:
Code:
Sheets excelSheet = excelWorkbook.Worksheets; // Fails since excelWorkbook is not defined in the scope of this method.
You should make excelWorkbook a class member instead.
Re: C# Excel Workbook Open
Thanks mariocatch for reply.
I have declare that in same button1_click. But everytime I click button1 extra excel workopen due to excelApplication.Workbooks.Open . My goal is to open workbook just once then run macro from timer sections . Because timer will eventually call excelApplication.Workbooks.Open at predefined interval set by me.
I just want run remaining part int say timer event after
sheets excelSheet = excelWorkbook.Worksheets;
Can you please suggest how do I access opened workbook for worksheet defination ?
Re: C# Excel Workbook Open
It sounds like you didn't do as I suggested. You only want the workbook to open once, so don't move the workbook open to your button_Click event. Keep it in your forms constructor. But, move the variable declaration outside of the constructor as a class member.
ie:
Code:
public class Foo
{
public Foo()
{
// Only open workbook once!
mWorkbook = new object();
mWorkbook.Open();
}
// Declare Workbook as class member.
private object mWorkbook;
private void button1_Click()
{
Sheets excelSheets = mWorkbook.Sheets;
}
}
Re: C# Excel Workbook Open
Ok, I will try your code. Let you know results how this gone.
Re: C# Excel Workbook Open
Hello mariocatch, thanks for your code help. It works perfect ! I moved applicationclass, workbook & sheet class out side of constructor as class member. Now I am able to open excelworkbook only once.