Larry said:
I wonder if someone could direct me to a simple use of an array. I
can't make any sense out of the material on arrays in Word 97 VBA
Help. I would just like to get a idea of what they are and what a
practical use of them would be.
Hi, Larry,
Think of an array as a table of values, all of the same data type. In the
simplest case, the table is only one row containing some sequence of cells.
Each cell has an index number, so you can go directly to it. If you define
the table to have two or more rows, then each cell has both a row index
number and a column index number.
As a very simple example, say you always open the same three files in the
morning, and you want to write a macro to automate that. You could just
repeat the same statement three times with different file names,
Documents.Open FileName:="c:\docs\alpha.doc"
Documents.Open FileName:="c:\docs\beta.doc"
Documents.Open FileName:="c:\docs\gamma.doc"
or you could create an array to hold the names and then use a For loop to
open them:
Dim FN(3) As String
Dim index As Integer
FN(1) = "c:\docs\alpha.doc"
FN(2) = "c:\docs\beta.doc"
FN(3) = "c:\docs\gamma.doc"
For index = 1 To 3
Documents.Open FileName:=FN(index)
Next index
or, equivalently,
Dim FN(3) As String
Dim index As Integer
FN(1) = "alpha"
FN(2) = "beta"
FN(3) = "gamma"
For index = 1 To 3
Documents.Open FileName:="c:\docs\" & FN(index) & ".doc"
Next index
Of course, this is overkill for three files, but it starts to make sense
when there are a lot more.
Arrays are also useful for populating list boxes and combo boxes in
userforms, and for sorting a list (if you don't want to insert the list in a
document and use TableSort).
Two-, three-, and greater-dimensional arrays are supported in VBA but are
relatively uncommon -- there just isn't that much need for them in a word
processing application.