Toggle macros

D

Dayo Mitchell

Word 2001, OS 9.2.2

Help! I'm trying to get a macro to toggle between showing and hiding the
Style Width column but I can't make it work. I already had two recorded
macros for showing and hiding the Style Width column, so I modified them,
and came up with a few variations that failed to work (inept macro code
below). VB Help on If...Else...Then statements has not been helpful.

I'd like to find a method I could then apply to toggling a number of
functions (e.g. show horizontal scroll bar, show status bar). Is
If...Then...Else even the way to go?

Dayo


First Try: Sub ToggleStyleWidth()

With ActiveWindow
If .StyleAreaWidth = InchesToPoints(1.5) Then
.StyleAreaWidth = InchesToPoints(0)
With .View
Else:
If .StyleAreaWidth = InchesToPoints(0) Then
.StyleAreaWidth = InchesToPoints(1.5)
With .View
End With
End If
End Sub

But VBE keeps telling me I have an Else without an If. (As does the example
in VBA Help, by the way). Then I tried an ElseIf statement:

Sub ToggleStyleWidth()

With ActiveWindow
If .StyleAreaWidth = InchesToPoints(1.5) Then
.StyleAreaWidth = InchesToPoints(0)
With .View
ElseIf .StyleAreaWidth = InchesToPoints(0) Then
.StyleAreaWidth = InchesToPoints(1.5)
With .View
End With
End If
End Sub

And now VBE highlights the *third* ".StyleAreaWidth" and says "Compile
error: Method or data member not found." (But the others seems okay).
 
J

J.E. McGimpsey

Dayo Mitchell said:
Word 2001, OS 9.2.2

Help! I'm trying to get a macro to toggle between showing and hiding the
Style Width column but I can't make it work. I already had two recorded
macros for showing and hiding the Style Width column, so I modified them,
and came up with a few variations that failed to work (inept macro code
below). VB Help on If...Else...Then statements has not been helpful.

I'd like to find a method I could then apply to toggling a number of
functions (e.g. show horizontal scroll bar, show status bar). Is
If...Then...Else even the way to go?

You can use If...Then...Else...End If, but in this case, since the
values are numeric, you can cheat a bit and use a math operation
instead.
Dayo


First Try: Sub ToggleStyleWidth()

With ActiveWindow
If .StyleAreaWidth = InchesToPoints(1.5) Then
.StyleAreaWidth = InchesToPoints(0)
With .View
Else:
If .StyleAreaWidth = InchesToPoints(0) Then
.StyleAreaWidth = InchesToPoints(1.5)
With .View
End With
End If
End Sub

But VBE keeps telling me I have an Else without an If. (As does the example
in VBA Help, by the way). Then I tried an ElseIf statement:

You were close. The reason you get a "Else without If" is that you
started another control structure ("With .View", but didn't end it
with "End With" so to the compiler, the structure looks like this:

Sub ToggleStyleWidth()
With ...
If ... Then
blah, blah
With ...
Else: <--- no "If" within With...End With
If ... Then
more blah, blah
With ...
End With
End If
End Sub

So to the compiler, you're missing an If...Then and End if (to match
the Else:), an End With (to match the second With...), and End If
(to match the first If...Then) and another End With (to match the
first With...)

One way to toggle would be:

Public Sub ToggleStyleWidth()
With ActiveWindow
If .StyleAreaWidth = InchesToPoints(1.5) Then
.StyleAreaWidth = 0
Else
.StyleAreaWidth = InchesToPoints(1.5)
End If
End With
End Sub


Somewhat simpler however, would be to alternately subtract 0" and
1.5" from 1.5" to get 1.5" and 0", respectively:

Public Sub ToggleStyleWidth()
With ActiveWindow
.StyleAreaWidth = InchesToPoints(1.5) - .StyleAreaWidth
End WIth
End Sub


Sub ToggleStyleWidth()

With ActiveWindow
If .StyleAreaWidth = InchesToPoints(1.5) Then
.StyleAreaWidth = InchesToPoints(0)
With .View
ElseIf .StyleAreaWidth = InchesToPoints(0) Then
.StyleAreaWidth = InchesToPoints(1.5)
With .View
End With
End If
End Sub

And now VBE highlights the *third* ".StyleAreaWidth" and says "Compile
error: Method or data member not found." (But the others seems okay).

Here you've used With .View, then used the shortcut .StyleAreaWidth.
But the .View object doesn't have a .StyleAreaWidth method or
property, so you get the error.
 
D

Dayo Mitchell

J.E., thanks much for the great tutorial. I have now compiled 3 toggle
macros that work and I even feel like I understand the
If...Then...Else...End If structure now.

Dayo
 
E

Elliott Roper

Dayo said:
J.E., thanks much for the great tutorial. I have now compiled 3 toggle
macros that work and I even feel like I understand the
If...Then...Else...End If structure now.

Dayo
...and you were not the only one hitched up for that ride. Thanks from
me too J.E.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top