Top
Tue Jul 27 09:18:09 CDT 2004
On 26 Jul 2004 04:03:46 -0700, cook_ml@hotmail.com (Martin) wrote:
>Top Spin <ToppSpin@hotmail.com> wrote in message
>
>> My new and improved code is: (critiques welcomed)
>>
>> Dim setting, reply
>>
>
>Have a look at this article from the Word MVP site
>
>
http://word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm
>
>which explains why it is better to declare variables with explicit data types.
Yes, I agree. Part of the reason was that it's just a quickie macro --
not a 5,000 module.
Also, I didn't know what the data type would be.
Is the result of "Selection.ParagraphFormat.SpaceBefore" Double?
Single? Integer? Long? ???
Is the result of MsgBox String?
I didn't want to spend the time to go figure it out and I knew that
Variant would handle anything.
Now, following your chastening, I did a little research. It appears
that "Selection.ParagraphFormat.SpaceBefore" returns a Single and
MsgBox a Long. Is that correct?
The Help file actually says that SpaceBefore returns a Single, but
check out this from the the MsgBox help:
=============== extract from VBA help file ==============
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "MsgBox Demonstration" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.
Else ' User chose No.
MyString = "No" ' Perform some action.
End If
=================================================
Do as I say, not as I do, I guess.
Maybe you should send your suggestion to the MSFT VBA help file
writers. ;-)
Anyway, here's the latest version of the macro:
Option Explicit
Sub SpaceBeforeToggle()
'=====================================================================
' Macro: SpaceBeforeToggle, Recorded on 07/21/04
'
' Toggle the Space Before paragraph setting between 0 and 6 points
' If it's not currently 0 or 6, ask the user for instructions
' If the selection contains different settings,
'=====================================================================
Dim setting As Single, reply As Long, msg As String
setting = Selection.ParagraphFormat.SpaceBefore
If setting = 6 Then 'If it's 6 now,
Selection.ParagraphFormat.SpaceBefore = 0 'Set it to 0
ElseIf setting = 0 Then 'If it's 0 now,
Selection.ParagraphFormat.SpaceBefore = 6 'Set it to 6
ElseIf setting > 999 Then 'If there are different
settings,
msg = "The selection contains multiple Space Before settings. " _
& "Set them all to 0?"
reply = MsgBox(msg, vbYesNoCancel + vbDefaultButton2,
"SpaceBeforeToggle macro")
If reply = vbYes Then 'If they say yes,
Selection.ParagraphFormat.SpaceBefore = 0 'Set it to 0
End If 'Otherwise, do nothing
Else 'Otherwise, ask the user
reply = MsgBox("Space before = " & setting & ", set it to 0?", _
vbYesNoCancel + vbDefaultButton2, "SpaceBeforeToggle macro")
If reply = vbYes Then 'If they say yes,
Selection.ParagraphFormat.SpaceBefore = 0 'Set it to 0
End If 'Otherwise, do nothing
End If
End Sub
How's that?
Thanks
--
Running Word 2K in Office 2K on Win2K-SR2
For email, use Usenet-20031220 at spamex.com