Consulting

Results 1 to 5 of 5

Thread: VBA logic help

  1. #1

    VBA logic help

    Hi --

    My question has less to do with actual syntax and more to do with the logical order that VBA uses to carry out loops & decision making statements. I've written what I thought made sense, but something isn't working right.

    Here is the logical process I want VBA to go through, in (mostly) plain English:

    Set intCurrentPrgOL = outline level of current paragraph

    Move up to previous paragraph.
    Set intNewPrgOL = outline level of new current paragraph [i.e., the one we just moved up to]
    If intNewPrgOL >= intCurrentPrgOL, then move up to the previous paragraph, set intNewPrgOL = outline level of that paragraph, and check again.

    If intNewPrgOL < intCurrentPrgOL, then go to the end of the line & change the [u] tag to a [Z] tag [all the ends of my lines have little tags like this]

    If intNewPrgOL <> 1, then move up to previous paragraph, and start the WHOLE process over again.

    If intNewPrgOL = 1, we're done for now.

    And here's the code that I thought would do that:

    [vba] Do Until intNewPrgOL = 1
    If intNewProgOL >= intCurrentPrgOL Then
    Selection.MoveUp Unit:=wdParagraph, Count:=1
    intNewPrgOL = Selection.Paragraphs(1).OutlineLevel
    ElseIf intNewPrgOL < intCurrentPrgOL Then
    Selection.EndKey Unit:=wdLine
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeBackspace
    Selection.TypeText Text:="Z"
    Selection.MoveUp Unit:=wdParagraph, Count:=2
    intNewPrgOL = Selection.Paragraphs(1).OutlineLevel
    End If
    Loop[/vba]

    But it always ends up changing the first few tags correctly, and then ending up in a paragraph with outline level 10 (normal text) & switching back and forth between the line

    [vba]If intNewProgOL >= intCurrentPrgOL Then[/vba]

    and

    [vba]ElseIf intNewPrgOL < intCurrentPrgOL Then[/vba]

    Can anyone see my mistake?

    Thanks!

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    This has everything to do with syntax really.

    VBA will use the logical order that you 'tell' it to

  3. #3
    A quick update --

    When I have use the debugger, it seems that intNewProgOL is getting the correct value assigned to it in every instance except the first time it loops. Then it shows that the value is empty.

    To fix this, I tried adding this line between the End If & Loop and then right after the Do.

    [VBA]intNewPrgOL = Selection.Paragraphs(1).OutlineLevel [/VBA]

    In both cases, the result was the same -- intNewPrgOL has the correct value every time, except in that first instance on the first time it loops back. Does anyone know why this is?

  4. #4
    Nevermind --

    I suppose it's not coincidence that the only place the value is empty is also the only place where the variable is spelled wrong. Sheesh.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If you are getting this....Use Option Explicit!!!!!!!!!!!!

    If you had Option Explicit set VBA would never let the code run in the first place. It would not tolerate mis-spelling....which is the point. The mis-spelled variable would be highlighted and you would get the message "Variable not defined."

    Use Option Explicit. It will save you much hair pulling.

Posting Permissions

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