Excel Automation

  • Thread starter Thread starter Kenneth Hutson
  • Start date Start date
K

Kenneth Hutson

Hi Group,
I want to fill a list box on my form with the names of all open Excel
workbooks. Since Excel is a multiple instance application, I need a way to
step through each instance to get all of the workbook names. I've looked at
getobject() but this returns only the first instance of Excel it finds. When
I use a Process class, I can see all instances of running Excel but I don't
know how to assign each process to a variable so that I can get at the
workbook names. Any ideas would be helpful.
Thanks,
Kenneth Hutson
San Antonio, TX
 
Interesting requirement. I can provide the answer to one part. When you get
the Excel process you are interested in, simple use the Marshal object in
..NET to hook into the Excel process. Find the active document and then the
workbook collection and iterate.

Someone else would need to help you on grabbing the processes and making
sure that a particular process corresponds to a a particular instance of
excel. That's about the extent of my knowledge.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Alvin Bruney - ASP.NET MVP said:
Interesting requirement. I can provide the answer to one part. When you
get
the Excel process you are interested in, simple use the Marshal object in
.NET to hook into the Excel process. Find the active document and then the
workbook collection and iterate.

Someone else would need to help you on grabbing the processes and making
sure that a particular process corresponds to a a particular instance of
excel. That's about the extent of my knowledge.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

Thanks for the response Alvin,
Once I have the Application object I believe I have the iteration part down.
Just wonder how I can use an Excel process id as a way into the Application
object.
Thanks,
Kenneth Hutson
San Antonio, TX
 
¤ Hi Group,
¤ I want to fill a list box on my form with the names of all open Excel
¤ workbooks. Since Excel is a multiple instance application, I need a way to
¤ step through each instance to get all of the workbook names. I've looked at
¤ getobject() but this returns only the first instance of Excel it finds. When
¤ I use a Process class, I can see all instances of running Excel but I don't
¤ know how to assign each process to a variable so that I can get at the
¤ workbook names. Any ideas would be helpful.

GetObject always returns the first instance that was launched so you actually have to refer to the
document name if you need to retrieve a specific instance:

xlApp = GetObject("Book2").Application

If you don't know the document name you can probably use API function calls to cycle through the
windows for each app and determine which are Excel and then parse the document name. I don't have a
VB.NET example, but the following may help:

http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/Enumerating_Windows/article.asp
http://vbnet.mvps.org/index.html?code/enums/enumwindowsdemo.htm


Paul
~~~~
Microsoft MVP (Visual Basic)
 
GetObject always returns the first instance that was launched so you
actually have to refer to the
document name if you need to retrieve a specific instance:

xlApp = GetObject("Book2").Application

If you don't know the document name you can probably use API function
calls to cycle through the
windows for each app and determine which are Excel and then parse the
document name. I don't have a
VB.NET example, but the following may help:

http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/Enumerating_Windows/article.asp
http://vbnet.mvps.org/index.html?code/enums/enumwindowsdemo.htm
Hey Thanks Paul,
Seems that even with the CLR, we still must resort to API calls to git 'er
done..
Kenneth Hutson
 
¤ > GetObject always returns the first instance that was launched so you
¤ > actually have to refer to the
¤ > document name if you need to retrieve a specific instance:
¤ >
¤ > xlApp = GetObject("Book2").Application
¤ >
¤ > If you don't know the document name you can probably use API function
¤ > calls to cycle through the
¤ > windows for each app and determine which are Excel and then parse the
¤ > document name. I don't have a
¤ > VB.NET example, but the following may help:
¤ >
¤ > http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows/Enumerating_Windows/article.asp
¤ > http://vbnet.mvps.org/index.html?code/enums/enumwindowsdemo.htm
¤ >
¤ Hey Thanks Paul,
¤ Seems that even with the CLR, we still must resort to API calls to git 'er
¤ done..
¤ Kenneth Hutson
¤

Yeah, and I forgot to mention one other fun issue: Excel doesn't necessarily register itself in the
ROT (Running Object Table) which will prevent GetObject from finding an instance. However, there is
a way to force Excel instances to register.

http://support.microsoft.com/default.aspx?scid=kb;en-us;155201


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top