Solved

VB.NET, Excel, Tracking Multiple Instances of

Posted on 2004-04-14
4
528 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
  • 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 200 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 64
Problem to With line 4 63
VB6 - Scroll Mouse wheel on Picturebox 13 77
adding "ungroup sheets" to existing vbs code 5 31
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

827 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