Results 1 to 10 of 10
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lesson in Loops, please! (WinNT4/Word97)

    I currently have several series of "If ... Then ... Else" statements that are prime candidates for putting into loops, so I'm trying to practice but setting these up - and I've done a couple already that work perfectly! <img src=/S/smile.gif border=0 alt=smile width=15 height=15> However, this last one is defeating me ...

    An example of the current If ... statements is as follows:

    Notes: "advar" is a variable replacement for "activedocument.variables" (to shorten the code lines), and "addreturn" and "takereturn" are small functions that add or remove returns from before the text in each case.
    <pre> If frmM2.chkBorBack = True Then
    adVar("addfBorBackt") = "Borrow Back"
    addReturn "addBorBackt"
    adVar("addfBorBack") = txtBorBack
    Else
    adVar("addfBorBackt") = " "
    takeReturn "addBorBackt"
    adVar("addfBorBack") = " "
    End If</pre>

    There are eight in each series, and the text "BorBack" in this If ... statement is the variable. My first attempt to 'loop' this is as follows, and based on the existing ones that do work:
    <pre> AddPlus = Array("BorBack", "Credit", "Under", "PayHol", "Unsec", "SecBor", _
    "LinkCur", "LinkSav")
    AddPlus2 = Array("Borrow Back", "Credit Card", "Under-payments", _
    "Payments Holidays", "Unsecured Borrowing", "Additional Secured Borrowing", _
    "Linked Current Accounts", "Linked Savings Accounts")
    For Each ii In AddPlus
    If frmM2.Controls("chk" + ii) = True Then
    adVar("addf" + ii + "t") = AddPlus2(ii)
    addReturn "add" + ii + "t"
    adVar("addf" + ii) = "txt" + ii
    Else
    adVar("addf" + ii + "t") = " "
    takeReturn "add" + ii + "t"
    adVar("addf" + ii) = " "
    End If
    Next</pre>

    However, there are two things in this one that are different - specifying the text from AddPlus2 to match the same item in AddPlus, and the last adVar line, which refers to a previously set variable, always called "txt" plus the looped variable (Borback). My attempts at these are both wrong, but I'm stuck as to what they should be!

    <img src=/S/help.gif border=0 alt=help width=23 height=15>, please!
    Beryl M


  2. #2
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    Hi Hans - thanks! I must admit almost everything seems to "confusion" me these days ... <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Just a couple of quick questions - (1) where do you define txtArray? The only place I can see it is when you use it, and (2) presumably it's okay if I continue to use ii instead of i? Only i is used elsewhere and gets a duplicate error if I use it here!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Beryl M


  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    You must have declared variables txtBorback, txtCredit etc. somewhere. Instead of this, declare an array

    Dim txtArray(0 To 7) As String

    and instead of assigning a value to txtBorback, assign it to txtArray(0); instead of assigning a value to txtUnder, assign it to txtArray(2), etc.

    Yes, you can replace i by ii or by any other unique name in the code I posted.

  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    Ah - got it! Yep, that does it (with one minor change: <font color=blue>If frmM2.Controls("chk" & i) = True Then</font color=blue> needed to be
    <font color=blue>... ("chk" & AddPlus(i)) = ...</font color=blue>, but that was just an oversight, I'm sure!).

    Thanks again!

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Beryl M


  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    > but that was just an oversight

    Yes, it was, sorry about that. I'm glad you caught it yourself.

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    <img src=/S/smile.gif border=0 alt=smile width=15 height=15> I've got a good teacher!
    Beryl M


  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    Does this do what you want? I used a For i = ... loop instead of a For Each loop. You will have to use an array instead of variables txtBorback etc., for Word VBA does not have a function to evaluate a variable indirectly. I named the array txtArray in the code. Oh, and I used the "official" concatenation operator & instead of +. The + operator will work too, but could confusion if you try to concatenate numeric values, since it is also the addition operator.
    <code>
    Dim i As Integer, AddPlus As Variant, AddPlus2 As Variant
    AddPlus = Array("BorBack", "Credit", "Under", "PayHol", "Unsec", "SecBor", _
    "LinkCur", "LinkSav")
    AddPlus2 = Array("Borrow Back", "Credit Card", "Under-payments", _
    "Payments Holidays", "Unsecured Borrowing", "Additional Secured Borrowing", _
    "Linked Current Accounts", "Linked Savings Accounts")
    For i = LBound(AddPlus) To UBound(AddPlus)
    If frmM2.Controls("chk" & i) = True Then
    adVar("addf" & AddPlus(i) & "t") = AddPlus2(i)
    addReturn "add" & AddPlus(i) & "t"
    adVar("addf" & AddPlus(i)) = txtArray(i)
    Else
    adVar("addf" & AddPlus(i) & "t") = " "
    takeReturn "add" & AddPlus(i) & "t"
    adVar("addf" & AddPlus(i)) = " "
    End If
    Next i</code>

    Note added later: "chk" & i should have been "chk" & AddPlus(i). See below in this thread.

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    Hi Hans - on a similar subject, how would I do the same thing with variables (defined as Public ... in another module)?

    I have a selection of these, all defined as "Public f3intro as boolean, f3existsec as boolean, etc", where all but the f3 in the name of each constitutes names used elsewhere and defined as a variable string for a function; ie in the controls("chk" + clicked) method, where 'clicked' is the variable replaced by 'intro', 'existsec' etc.

    I need to define something similar to the controls(...) etc bit, but using the Public variables - what do I call them?! I know they're not controls (at least ... they're not, are they?!) and I've tried activedocument.variables(...) and variables(...), neither of which worked; and I've run out of ideas!

    Any help gratefully appreciated!
    Beryl M


  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    I'm sorry, it just isn't possible in Word VBA. A module doesn't have a Variables collection, and there is no Eval function that evaluates a name passed as a string.

    Alternatives:
    - Store the values temporarily in document variables - the Document object does have a Variables collection. Note: this collection has NOTHING to do with the variables in a module. Document variables are entirely distinct from VBA variables, although you manipulate them in VBA.
    - Store the values in invisible controls on a userform, so that you can use the Controls collection.
    - Use an array - not very intuitive in your situation.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Lesson in Loops, please! (WinNT4/Word97)

    Why do you need this??

    Another possible option is to create your own myVariables collection, i.e., as a VBA "Collection". The "key" would be the name of the variable and the "item" would be its value.
    <UL>Dim myVars As New VBA.Collection
    With myVars
    .Add "Hello world!", "String1"
    .Add 27.5, "RealNumber1"
    .Add ActiveDocument, "DocObject1"
    End With
    Stop ' View > Locals to see if it worked
    Set myVars = Nothing[/list]
    The members of the collection end up being variants of various types, which can be bad for debugging, and because the individual variables are not declared, you do not have the benefit of intellisense fixing up the capitalization as a cue to typos. Worse yet, I don't see how you can retrieve the keys. Hmmm... maybe this is where the VBScript Dictionary object comes in handy?

    Never mind. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •