Solved

VB.NET, Excel, Tracking Multiple Instances of

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now