PDA

View Full Version : VBA logic help



AKK
07-25-2007, 10:49 AM
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
If intNewPrgOL >= intCurrentPrgOL, then move up to the previous paragraph, set intNewPrgOL = outline level of [I]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:

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

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

If intNewProgOL >= intCurrentPrgOL Then

and

ElseIf intNewPrgOL < intCurrentPrgOL Then

Can anyone see my mistake?

Thanks!

Norie
07-25-2007, 10:54 AM
This has everything to do with syntax really.

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

AKK
07-25-2007, 11:23 AM
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.

intNewPrgOL = Selection.Paragraphs(1).OutlineLevel

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?

AKK
07-25-2007, 11:46 AM
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.

fumei
07-26-2007, 06:38 AM
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.