9

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
gNerb
  • 867
  • 2
  • 12
  • 28
  • 2
    normally, at least in VBA, if you don't know the amount of elements at compile time you go with a [Collection](http://www.wiseowl.co.uk/blog/s239/collections.htm) or [Dictionary](http://stackoverflow.com/questions/915317/does-vba-have-dictionary-structure) or [CustomCollection](http://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba/19379641#19379641). –  May 28 '14 at 15:37
  • I'm not really familiar with the VB data structures. In C++ there are several different dynamically sizable data structures. In this case a linked list would be perfect. I don't think there is anything really similar but a quick look at collection tells me it might be a good choice. Thanks for the info. – gNerb May 28 '14 at 17:09

2 Answers2

12
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 ...
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 1
    That got rid of the error. Of course it would be that simple :) thanks! I will accept the answer when the timer is up. – gNerb May 28 '14 at 14:51
  • 3
    (Also worth noting that attempting to access an array of this type before its been `redim`d will result in an error) – Alex K. May 28 '14 at 14:54
  • 1
    I call ReDim immediately after I create the array specifically to avoid this. – gNerb May 28 '14 at 17:06
4
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))
Anurag_BEHS
  • 1,390
  • 2
  • 22
  • 36