Hi,
I have a requirement to check if an excel document is open or closed
from another C# application. For this, I am using Automation to get the
instanc of the Excel application and iterating through the WorkBooks
collection.

The problem is sometimes the Workbooks collection returns empty
collection and so I could determine if the document is open or not.
Some time it works correctly with the same environment.

Here is my code snippet I am using.

if(excelApp == null)
{
try
{
excelApp =
(Excel.Application)Marshal.GetActiveObject("Excel.Application");
}
catch(System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
if (excelApp == null) return null;
}

foreach(Excel.Workbook book in excelApp.Workbooks)
{
try
{
if (book.FullName.ToLower() == m_strFileName.ToLower())
{
return book;
}
}
catch(System.Exception ex)
{
//You will get the exception here if the application is busy/
RMLogger.Error(ex);
return null;
}
}

Any help is highly appreciated.

Thanks
Thanga