Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using Excel = Microsoft.Office.Interop.Excel;
namespace program
{

	private void cmdOpen_Click(object sender, System.EventArgs e)
	{

	FileSystem.FileOpen(1, inputfile, OpenMode.Input, (OpenAccess)(-1), (OpenShare)(-1), -1); // 1 : excel file
	Excel.Application MyXl;
	MyXl = new Excel.ApplicationClass();
	MyXl.Visible = true;
	Excel.Workbook xlWorkbook = MyXl.Workbooks.Open(inputfile, 0, false, 5,"", "", fal<small></small>se, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
        Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets;
	Variables();	

	}

	public void Variables()
	{
	int i; 
        object ws; 
        int j;
	ws = MyXl.xlWorksheet("sheet1");
	do
		{
		
		inputs[i] = ws.Cells[5, input_startcol+i];
	
		}while(inputs[i]!="");

	}



Error:
1.The name 'MyXl' does not exist in the current context
2.'object' does not contain definition for 'Cells'
Posted
Updated 12-Dec-11 22:05pm
v2
Comments
NikulDarji 13-Dec-11 4:03am    
Globally declare the "MyXl" object....
surakesh 13-Dec-11 4:34am    
thanks,,
surakesh 13-Dec-11 6:54am    
Whether is MyXl declared globally or not , unable to use xlWorksheet in the Variables() function .
uspatel 13-Dec-11 4:06am    
Pre tag added.

MyXl is declared in cmdOpen_Click and should be passed to Variables, otherwise it will be undefined.
public void Variables(Excel.Application MyXl)

Also, about the next line:
object ws;

Use specific type declaration for other excel classes like workbooks and worksheets:
Excel.Workbook wb;
Excel.Worksheet ws;

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900