PDA

View Full Version : Solved: ReDim 2 dimensional array error Help



lestng
03-17-2006, 12:30 AM
I have the following script going through a document and collect the paragraph text and url and store them in a two dimensional array but got the following error on the last ReDim statement:

Runtime error '9'
Subscript out of range.

This is my first time dealing with 2 dimensional table would really appreciate to get some help.

Sub LoopParagraphs()

Dim HyperlinkArray()
ReDim HyperlinkArray(100, 2)
Dim oRow As Integer
Dim para As Paragraph

oRow = 0
Selection.HomeKey Unit:=wdStory
For Each para In ActiveDocument.Paragraphs
If para.Style = "item:1head" Or para.Style = "item:title" Or para.Style = "item:2head" _
Or para.Style = "item:2" Then
If para.Range.Hyperlinks.count >= 1 Then

HyperlinkArray(oRow, 1) = para.Range.Hyperlinks.item(1).Address
End If
HyperlinkArray(oRow, 0) = para.Range.Text
oRow = oRow + 1

End If
Next para
ReDim Preserve HyperlinkArray(oRow, 2)
End Sub

mdmackillop
03-17-2006, 12:34 AM
Hi Lestng,
Check out Redim in your VBA help files.
Regards
MD

Quote: If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

lestng
03-17-2006, 09:37 AM
Hi Lestng,
Check out Redim in your VBA help files.
Regards
MD

Quote: If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

Hi mdmackillop
Thanks for your quick response as always. Now I know how that Preserve keyword works.

I have resolved it by first looping through the doc to determine the oRow and ReDim the array after the loop. I am one of those people who can only sees the first subscript as the ROW and so on. That is why it took me this long to come only this far on my Printing Macro Task. I have reused your logic mostly. But I am having fun learning.

Any luck on your Print Macro which got lost only on a specific pc? Pardon me for not checking out that thread if you already resolved that problem.

Thanks for your help.

p/s now I have to figure out how to mark this thread as Resolved

mdmackillop
03-17-2006, 12:30 PM
There is an updating going on which has mislaid Solved, so I'll do it for you.
Re my print problem, I've a couple of basic problems on this PC. I'm afraid it's Format C time. :mkay