I have the following problem. I have several tables in a document and I need
to work with the tables in a certain style. So I wrote a piece of code that
loops through the tables, checks te style and if it is a match creates a
variable in the variable collection of which the name is a counter for the
matches and the value is a counter for the tables. This works fine, but when
the matches exceed 10 the following occurs:

While looping through my variables by itemnumber the order is changed. I use
the following code:
For i = 1 To ActiveDocument.Variables.Count
iTmp = ActiveDocument.Variables(i).value

and now expect to see my matches from 1,2,3,....,9,10,11... and so on...
however I get: 1,10,11,12,..,2,3,4...
Am I doing something wrong?

Re: looping through variables by Jay

Jay
Wed Oct 26 07:05:34 CDT 2005

On Wed, 26 Oct 2005 04:37:04 -0700, "Steve"
<Steve@discussions.microsoft.com> wrote:

>I have the following problem. I have several tables in a document and I need
>to work with the tables in a certain style. So I wrote a piece of code that
>loops through the tables, checks te style and if it is a match creates a
>variable in the variable collection of which the name is a counter for the
>matches and the value is a counter for the tables. This works fine, but when
>the matches exceed 10 the following occurs:
>
>While looping through my variables by itemnumber the order is changed. I use
>the following code:
>For i = 1 To ActiveDocument.Variables.Count
> iTmp = ActiveDocument.Variables(i).value
>
>and now expect to see my matches from 1,2,3,....,9,10,11... and so on...
>however I get: 1,10,11,12,..,2,3,4...
>Am I doing something wrong?

Yes, you are. <g>

The Variables collection is being returned to you in alphabetic order,
not in the order of creation. To work around this, name the variables
with two digits: 01, 02, 03... 10, 11, ... It's simplest to use the
Format function to create these names.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org