Top Spin said:
Anyway, here's the latest version of the macro:
How's that?
Yes, you're right, SpaceBefore does return a Single; my help file
(Word 2000) says that MsgBox returns an integer, not a long, *but*
strictly speaking it returns a vbMsgBoxResult, which is an enumerated
type containing the values vbYes, vbNo etc, which are all just
constants representing numeric values.
You can see this if you hit F2 within the VBA Editor which will open
up the object browser. Type msgbox into the Search Text box and click
on the binoculars to search. Within the Search Results pane click on
the line which has Class Interaction, Member MsgBox. Then at the very
bottom of the screen you will see the definition of the function,
including the return type "As VbMsgBoxResult". If you click on that
hyperlink the lower pane will then show you the members and if you
click on one of them you can see at the buttom what actual numeric
value each of the constants represents.
In your code if you
Dim reply as vbMsgBoxResult
then as soon as you type
If reply =
you will get a popup menu containing all the members of the type,
which you can then select one from.
I agree too that some of the examples in the VBA/VB help files are
dreadful because of their practice of leaving out the variable types!!
You're obviously keen so may I be picky with your code? :
* Why have Yes, No and Cancel buttons in your message boxes, when
you're only interested in Yes or No?
* I would make the string "SpaceBeforeToggle macro" into a constant;
then if you want to change it you only have to do so in one place, not
search and replace (obviously in this tiny macro it's not a great job
to do so but I am talking good practice here!)
Having started to think about things I realised that I would do a few
things differently and I ended up with the following :
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 sngSetting As Single, strMsg As String
' Store the existing setting
sngSetting = Selection.ParagraphFormat.SpaceBefore
Select Case sngSetting
Case 6
sngSetting = 0
Case 0
sngSetting = 6
Case Is > 999 'If there are different settings,
strMsg = "The selection contains multiple Space Before
settings. Set them all to 0?"
Case Else
strMsg = "Space before = " & sngSetting & ", set it to 0?"
End Select
If Len(strMsg) <> 0 Then ' If there is a message to display
If MsgBox(strMsg, vbYesNo + vbDefaultButton2 + vbInformation,
"SpaceBeforeToggle macro") = vbYes Then
sngSetting = 0
End If
End If
' Set to new setting (or existing if no change is to be made)
Selection.ParagraphFormat.SpaceBefore = sngSetting
End Sub
You'll notice that
* I have used a Select Case statement which to me is clearer to read
* there is more white space, again making it nicer to read
* there is now only one MsgBox statement, so the need for a constant
has gone away
* that I don't assign the return value from the MsgBox function to an
intermediate variable but just test it directly
* my variable names are prefixed so that it is immediately clear what
datatype they are
Your code worked fine, and some of what I have done is purely
stylistic. I hope it gives you some different ideas which you can use
in the future if you choose. If you, or anyone else, would like to
argue about how I have done things that will be just fine
Martin