Solved

VB.NET, Excel, Tracking Multiple Instances of

Posted on 2004-04-14
4
541 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

632 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