Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array already dimensioned - Class module

For certain, technical, reasons, we cannot use styles in word. In an effort to speed up applying global properties over and over, I've created a class that can read from a simple xml style-sheet. The sheet contains different "paragraphs." Each paragraph simply stores the paragraph properties that we use the most.

I'm used to C++ where I can use dynamic memory and I'm trying to replicate the behavior of a dynamically allocated array. However, when I attempt to re-dim I get the error message "Array arleady dimensioned."

My research on the MSDN suggests that in order to ReDim the array has to be Global or in the "general declaration context" This makes me think it might simply not be possible to do it in a class.

Excerpt from MSDN:

"You can use ReDim only at procedure level. Therefore, the declaration context for the variable must be a procedure; it can't be a source file, a namespace, an interface, a class, a structure, a module, or a block."

I have attempted to search stack overflow for "Word VBA Array already dimensioned" and went through all 3 pages of results with no avail.

private type pStyle 'Definition removed because it's not needed
private Paragraphs(0) As pStyle 'Initially an empty array of paragraphs

later I have the following function

Public Function AddEmpty()

'Create space
count = count + 1
ReDim Preserve Paragraphs(count)
AddEmpty = count
End Function

Please let me know if any ideas. I would prefer to not have to "estimate" the number of paragraph styles we will need for each style sheet as every file is different.

like image 831
gNerb Avatar asked May 28 '14 14:05

gNerb


2 Answers

Private Paragraphs(0) As ...

This is not an empty array, rather it is a fixed length array with 1 element.

For a dynamic array - one you will later redimension - just declare it as:

Private Paragraphs() As ...
like image 198
Alex K. Avatar answered Oct 10 '22 01:10

Alex K.


Dim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))

Above code will throw array-already-dimensioned.
we can do like

ReDim numbers(10) As Integer
MsgBox (UBound(numbers))
ReDim numbers(4)
MsgBox (UBound(numbers))
like image 33
Anurag_BEHS Avatar answered Oct 10 '22 03:10

Anurag_BEHS