Cannot get the Worksheet I want

Hi there,
I'm making some test in order to work with an Excel file. Problem is when I want a specific worksheet dosn't work.
It always show me the last active worksheet before closing the file..

Any suggestion?

Roberto
internal void openExcel(String fileName)
{
  int updateLink = 2;
  bool readOnly = true;
  int format = 5;
  string password = "";
  string writeResPwd = "";
  bool ignoreReadOnly = true;
  object origin = Excel.XlPlatform.xlWindows;
  string delimiter = "\t";
  bool editable = true;
  bool notify = false;
  int converter = 0;
  bool addToMru = true;
  bool local = true;
  object corruptLoad = Excel.XlCorruptLoad.xlNormalLoad;
  string currentSheet = "IN181109_1";

			   
  //File opening
   xlWb = xlApp.Workbooks.Open(fileName,updateLink,readOnly,format,password,writeResPwd,ignoreReadOnly,origin,delimiter,editable,notify,converter,addToMru,local,corruptLoad);
          
  //Get the sheets
    xlSheets = (Excel.Sheets)xlApp.ActiveWorkbook.Worksheets;
		 
  //Problem is here I don't get the correct one.. try even with get_Item(2) but same result		 
    xlWorkSheet = (Excel.Worksheet)xlSheets.get_Item(currentSheet);

    xlApp.Visible = true;
    xlApp.UserControl = true;
}

Open in new window

gokyo66Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
just assigning the value to "xlWorkSheet" won't open that specific work sheep, you may need to explicitly call the Visible/Open (this part I need to check) to show that specific worksheet
0
gokyo66Author Commented:
Not sure abt what you said, because all the example I check before writing here shows that with the get_Item(object index) method you can reach the worksheet you want
0
Roshan DavisCommented:
try
xlWorkSheet.Select();

OR

xlWorkSheet.Activate();

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Roshan DavisCommented:
or try
xlWorkSheet.Active = true;

Open in new window

0
gokyo66Author Commented:
Thank you, roshmon
using the Activate method works... only I got this warning I don't know how to fix it... consider that Im pretty new in C# and related

HandleExcel.cs(43,18): warning CS0467: Ambiguity between method  Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method
        'Microsoft.Office.Interop.Excel.DocEvents_Event.Activate'. Using method group.
c:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\10.0.4504.0__31bf3856ad364e35\Microsoft.Office.Interop.Excel.dll: (Location of symbol related
        to previous warning)

0
Roshan DavisCommented:
can you try this?
((Excel.Worksheet)xlWorkSheet).Activate();

Open in new window

0
gokyo66Author Commented:
nope...

let me include more info about how I created this class maybe I did something wrong...let say Im pretty sure I did something wrong...

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace MyTestExcel
{
  class HandleXL
  {
   private static Excel.Application xlApp;
   private static Excel.Workbook xlWb; //
   private static Excel.Sheets xlSheets;
   private static Excel.Worksheet xlWorkSheet;
		
   internal void openExcel(String fileName)
   {
     int updateLink = 2;
     bool readOnly = true;
     int format = 5;
     string password = "";
     string writeResPwd = "";
     bool ignoreReadOnly = true;
     object origin = Excel.XlPlatform.xlWindows;
     string delimiter = "\t";
     bool editable = true;
     bool notify = false;
     int converter = 0;
     bool addToMru = true;
     bool local = true;
     object corruptLoad = Excel.XlCorruptLoad.xlNormalLoad;
     string currentSheet = "IN181109_1";

			   
xlWb = xlApp.Workbooks.Open(fileName,updateLink,readOnly,format,password,writeResPwd,ignoreReadOnly,origin,delimiter,editable,notify,converter,addToMru,local,corruptLoad);
          
xlSheets = (Excel.Sheets)xlApp.ActiveWorkbook.Worksheets;
xlWorkSheet = (Excel.Worksheet)xlSheets.get_Item(currentSheet);
((Excel.Worksheet)xlWorkSheet).Activate();


xlApp.Visible = true;
xlApp.UserControl = true;
}
		
 public HandleXL()
 {
    xlApp = new Excel.Application();
 }
		
	//main method
	public static void Main()
	{
	   HandleXL hXL = new HandleXL();
	   hXL.openExcel(@"D:\Sviluppo SAP\PackingList\IN181109.XLS");
	}
}
 

}//end namespace MyTestExcel

Open in new window

0
Roshan DavisCommented:
the following should solve the problem
(xlWorkSheet as Microsoft.Office.Interop.Excel._Worksheet).Activate();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roshan DavisCommented:
or
(xlWorkSheet as _Worksheet).Activate();

Open in new window

0
gokyo66Author Commented:
Man...
thank you very much...you got it...

Appriciate your help

Roberto
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.