View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Adding worksheets

This is the code from the Help "Add Mathod" in VBA. The new is inside the do
loop which adds new memory for each item. The original question did give
enough information to tell me if warbornster needed to keep on adding
addtional memory or if he wanted to reuse the object. Instead of doning
adds, he could of done copy and minimized the amount of memory the program
used. I just wanted to point out what the new actually did (add memory). A
lot of programming mistakes are made because memory isn't properly assigned
and over-flows occur.

Dim MyClasses As New Collection ' Create a Collection object.
Dim Num As Integer ' Counter for individualizing keys.
Dim Msg
Dim TheName ' Holder for names user enters.
Do
Dim Inst As New Class1 ' Create a new instance of Class1.
Num = Num + 1 ' Increment Num, then get a name.
Msg = "Please enter a name for this object." & Chr(13) _
& "Press Cancel to see names in collection."
TheName = InputBox(Msg, "Name the Collection Items")
Inst.InstanceName = TheName ' Put name in object instance.
' If user entered name, add it to the collection.
If Inst.InstanceName < "" Then
' Add the named object to the collection.
MyClasses.Add item := Inst, key := CStr(Num)
End If
' Clear the current reference in preparation for next one.
Set Inst = Nothing
Loop Until TheName = ""
For Each x In MyClasses
MsgBox x.instancename, , "Instance Name"
Next



"Tom Ogilvy" wrote:

Joel,

There isn't enough
code to tell what you are doing to give suggestions.


Im trying to add a number of
sheets to a workbook in vsto (c#).


there is the beauty of it. Now since you understand exactly what he wants to
do, why not put up your own C# code to show him how to do it properly.

--
Regards,
Tom Ogilvy




"Joel" wrote:

new is object oriented code to create a new class (object). The new
sttatement assigns memory space for the class. You can't add moe than one
item to the object because there isn't enough memory. There isn't enough
code to tell what you are doing to give suggestions.

" wrote:

Hello, I guess this is kind of basic stuff, but I would be very
pleased if you could help me anyway. Im trying to add a number of
sheets to a workbook in vsto (c#). I use a list to add several sheets.
the interesting code is pretty much:

List<Excel.Worksheet ws;

ws = new List<Excel.Worksheet();
for (int i = 0; i <= theend; i++)
{

ws.Add((Excel.Worksheet)Globals.ThisWorkbook.Works heets.Add(missing,
missing, missing, missing));
//a lot of code after this:

}

This work the first time in the loop, the other time though I get an
ArgumentOutOfRangeException when it hits any of the ws objects. I may
have done this completely wrong, but Im new with this so have patience
plz ; )