?
Solved

VB.NET, Excel, Tracking Multiple Instances of

Posted on 2004-04-14
4
Medium Priority
?
549 Views
Last Modified: 2008-02-26
I want to do something like this:

Dim appExcel As Excel.Application
Dim wBook As Array of Excel.Workbook  ' wrong
Dim wSheet As Excel.Worksheet

appExcel = openExcel()
wBook.Add (objXL.Workbooks.Add)           ' wrong
wSheet = wBook.Item(0).Worksheets(1)    ' wrong

Friend Function openExcel() As Excel.Application
        Dim appExcel As Excel.Application
        On Error Resume Next
        appExcel = GetObject(, "Excel.Application")
        If Err.Number = 0 Then
        Else
            appExcel = CreateObject("Excel.Application")
            Err.Clear()
        End If
        On Error GoTo 0
        Return appExcel
End Function

but that is obviously not correct. I will not know how many workbooks need to be created as part of the overall application. The openExcel part works fine and I can restrict opening of excel to one process but I can't seem to find the right way to define a array / collection / whatever of excel.workbook's.

This is in relationship to question posed in http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20947041.html

Help.
0
Comment
Question by:Knomaze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10826162
Notice how the Excel object was used in the other question:

        Dim objXL As Excel.Application
        Dim objWB As Excel.Workbook
        Dim objWS As Excel.Worksheet

        objXL = New Excel.Application
        objWB = objXL.Workbooks.Add
        objWS = objWB.Worksheets(1)

you do NOT create a direct instance of the WorkBooks object itself, in fact you CAN'T.  Rather you use the internal instance of the WorkBooks object as a FACTORY class, to create individual Workbook objects using the WorkBooks.Add method.  Does that make it clearer for you?

AW
0
 

Author Comment

by:Knomaze
ID: 10826420
Nope - This part I already know as its my code.

I want to keep track of the workbooks that I open and uniquely identify them without having to save them to disk first. I'd like to be able to track of them in a list or array or some such collection that I can simply just add additional elements to or take elements away from as I add and remove workbooks from the excel application.

I'd love to just be able to say

Dim objWB() As Excel.WorkBook

and just keep adding workbooks to the array but I see that VB.NET doesn't allow you to do this even for basic element types like strings - or at least not that I've been able to get to work. Unfortunately my VB books which explains alot of this stuff are sitting where I can't get to them and the online resources for VB.NET seem non-existent.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 800 total points
ID: 10826483
create objWB as an ArrayList  and then you can add instances of the WorkBook object to the ArrayList. like this:

Dim objWB as New ArrayList

objWB.Add(objXL.Workbooks.Add)
0
 

Author Comment

by:Knomaze
ID: 10826586
WOO HOO!

I thought I tried that already - must have mis-typed something because it certainly wasn't working yesterday. Seems to be okay now - user error I guess.

Thanks,
K
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question