[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

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

0
gokyo66
Asked:
gokyo66
  • 6
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now