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

Hi Thanga,

Thanks for coming back on this, I'm even more intrigued! One side of my
brain is telling me it can't work, ie end up with a ref to each multiple
instance, yet I'm beginning to see how it might. I have Visual Studio but
not yet been able to compile into something useable. My lack of C# is
letting me down, need to brush up which I fear will take a while.

The subject of referencing multiple unknown instances often crops up (not
directly your question) so it'd be handy if a DLL can be created that can be
used in VB/A to return, say, an array of object ref's to all Excel
instances.

My VB method works reasonably well but not quite well enough to be
distributed at large. As I mentioned before -

- Builds an array of Excel Win handles (XLMAIN) together with all unsaved
files named Book# in each instance (EXCEL7).
- If an instance does not have a Book#, add a new Book# to that instance
with DDE (hide this for future use).
- Use GetObject("Book#").Parent to reference each instance.

If you or anyone is interested to see my VB/VBA I'll forward a demo workbook
(contact below). But your C# method looks potentially much better!

Regards,
Peter T
pmbthornton gmail com

PS, in your adjacent you said

Why don't you post
your code snippet and see if I can help you with it.


I don't have a snippet, only what you posted previously, but thanks


"Thanga" wrote in message
oups.com...
Peter,
I have modified my code snippet to iterate through all the instance and
get the work books loaded in each instance. You can view the Excel
registration in the ROT using "IROTVIEWER" which comes with the Visual
studio.

protected void GetObjectFromROT()
{
Hashtable runningIDEInstances = new Hashtable();
Hashtable runningObjects = GetRunningObjectTable();

IDictionaryEnumerator rotEnumerator =
runningObjects.GetEnumerator();
while ( rotEnumerator.MoveNext() )
{
string candidateName = (string) rotEnumerator.Key;
if

(candidateName.ToLower().startswith("!{0024500-0000-0000-C000-000000000046}"
)
{
//Get the Excel Application Handle.
//Iterate through the list of Worksbooks.
//Print the file name.
}
}


return null;
}