Home |
Search |
Today's Posts |
|
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
I forgot my password to open an excel document, how do I open it? | Excel Discussion (Misc queries) | |||
When I open Excel, workbooks open automatically. How can I stop t | Excel Discussion (Misc queries) | |||
my excel document won't open unless i go file, then open | Excel Discussion (Misc queries) | |||
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 | Excel Programming | |||
Workbooks.Open / .Open Text - How do you stop the .xls addition? | Excel Programming |