0

excel object returns count zero Please refer the screenshot.

I need to access objects of file type PPT, Excel, Word which are opened through an application similar to sharepoint.

While I try getting count of opened excel files I always get the count as 0. I already searched if I can get help, got to see this Accessing an open Excel Workbook in C# but though implementing the solution it returns 0.

Microsoft.Office.Interop.Excel.Application xlObj =
(Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
 while (xlObj.Windows.Count == 0) // This line returns the count as 0
Community
  • 1
  • 1
  • *"opened through an application similar to sharepoint"* > Are the files opened / viewed in a web browser, or are the files being downloaded and then opened in the Microsoft Office application? – bassfader Mar 05 '17 at 08:43
  • The files are downloaded and opened in ms office. – Rashmi Gopinath Mar 05 '17 at 09:55
  • Any thoughts why this is happening? it works fine for Ppt and word objects. Also an observation, if I want to get focus on word and ppt application, I can use wordObj.Activate() and pptObj.Activate() why can't I do for excel ? xlObj.Activate() is not available – Rashmi Gopinath Mar 08 '17 at 07:20
  • The `Activate()` method is only available for Workbooks in Excel, not the whole application. Have you tried checking if there are any open Workbooks instead of checking the window count (i.e. check `xlObj.Workbooks.Count`)? If the count is greater than 0 then you should be able to activate the first workbook by calling `xlObj.Workbooks[1].Activate()` – bassfader Mar 08 '17 at 14:44
  • Thanks bassfader. I restarted the system excel count works fine now. Not sure what was the issue. I had release the COM objects, handled GC, Anyways my hard luck when it was not working. And about activating the excel file, xlObj.Workbooks[1].Activate() works fine. Thanks a lot. – Rashmi Gopinath Mar 10 '17 at 12:32

1 Answers1

0
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\nagendra.k.kumar\Desktop\Talent Review - Nagendra Kumar.xlsx");
        Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Microsoft.Office.Interop.Excel.Range xlRange = lWorksheet.UsedRange;
        MessageBox.Show(xlRange.Rows.Count.ToString());