Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Workbooks collection is empty even though the some of the document is open

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 233
Default Workbooks collection is empty even though the some of the document is open

Thanga,

you wont be able to determine which Excel instance you are looking
at!(when there is more than 1 excel instance active).

Mysheet = GetObject(,"C:\myfile.xls") will connect to the first excel
instance that has this file open, or else it will open the file.

Dm Unseen

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Workbooks collection is empty even though the some of the document is open

Hi,
I just now saw that there are two instance of the excel is running when
this problem occurs.

Do you have any idea how to workaround this condition? May be by
getting all the instances?. I know getobject will return the first
instance it returns.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Workbooks collection is empty even though the some of the docu


Do you have any idea how to workaround this condition? May be by
getting all the instances?. I know getobject will return the first
instance it returns.


I'm not sure if C#'s GetActiveObject and VB's GetObject are the same beast,
but it certainly sounds like they are (warts and all !). If so, the
following thread discusses a way to return a specific instance.

http://groups.google.com.au/group/mi...db4ab107e4a7e/


Regards,
Vic Eldridge
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 233
Default Workbooks collection is empty even though the some of the document is open

There is in normal VB(A) to my knowledge no easy way of getting all
instances(And I looked really hard). It *is* possible using the ROT and
doing some hardcore API stuff, but this is really difficult.

What I did is create an out of process single instance ActiveX, which
is connected to by the XL file and my other VB app(in my case an Access
db). This way I can pass Object handles, so I always have the correct
Excel instance, the one that registered it's handle through my ActiveX.

Note that in .NET (i.e. c#) you might have some extra options, but that
solution would still need to look and the ROT(Running Object Table).

DM Unseen



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Workbooks collection is empty even though the some of the document is open

As you say not easy to grab all unknown Excel instance's but there are ways.
My method highly simplified:

Assumes that no unsaved BookX in any number of instances has the same suffix
X
Enumerate XLMAIN windows looking if any child windows in that instance has a
BookX, if so grab it with GetObject and ref it's xl parent

If it doesn't, bring the XLMAIN to the front (API) then with (createobject
invisible) Word use DDE to add a new workbook named BookX. Grab that, hide
the new BookX to save time if need to do again.

Normally possibly to create withevents xlApp class's of each instance from
which one can do whatever (read or manipulate).

Simple in theory but, as I mentioned in the link referred to by Vic
(adjacent post) convoluted and complicated!

Regards,
Peter T


"DM Unseen" wrote in message
oups.com...
There is in normal VB(A) to my knowledge no easy way of getting all
instances(And I looked really hard). It *is* possible using the ROT and
doing some hardcore API stuff, but this is really difficult.

What I did is create an out of process single instance ActiveX, which
is connected to by the XL file and my other VB app(in my case an Access
db). This way I can pass Object handles, so I always have the correct
Excel instance, the one that registered it's handle through my ActiveX.

Note that in .NET (i.e. c#) you might have some extra options, but that
solution would still need to look and the ROT(Running Object Table).

DM Unseen



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Workbooks collection is empty even though the some of the document is open

Hi All,
Thanks for your replies. I have solved this problem using ROT and
working pretty good without any issue. Access ROT is also not much
difficult if you know how to us P/Invoke.

Thanks
Thanga

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I forgot my password to open an excel document, how do I open it? chris ohearn Excel Discussion (Misc queries) 1 August 11th 08 02:32 PM
When I open Excel, workbooks open automatically. How can I stop t Rhealbird Excel Discussion (Misc queries) 2 February 23rd 06 10:08 AM
my excel document won't open unless i go file, then open EB500 Excel Discussion (Misc queries) 1 June 3rd 05 06:42 PM
Excel 2003 Workbooks.Open with CorruptLoad=xlRepairFile fails on Excel 5.0/95 file due to Chart, with Error 1004 Method 'Open' of object 'Workbooks' failed Frank Jones Excel Programming 2 June 15th 04 03:21 AM
Workbooks.Open / .Open Text - How do you stop the .xls addition? Dave[_20_] Excel Programming 2 July 31st 03 04:03 AM


All times are GMT +1. The time now is 12:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"