Finished Macro: Increment/Decrement Paragraph Spacing

T

Top Spin

I want to thank everyone who helped me get this little macro working.
I am posting the completed macro in case it might be useful to anyone.
I would be interested in any comments on the coding or logic.

If it is not appropriate to post code, please advise and I won't do it
again.

Thanks



'=====================================================================
' Macro: ParaSpaceBefDec6pts, Recorded on 07/21/04
'
' Assigned to Ctrl+Shift+{ 'Parallels Ctrl+[
'
' Decrease the paragraph Space Before setting by 6 pts (-6).
' See also ParaSpaceBefInc6pts
'=====================================================================
Sub ParaSpaceBefDec6pts()
Const sMacName As String = "ParaSpaceBefDec6pts"
Call ParaSpaceBef6pts("-6", sMacName)
End Sub


'=====================================================================
' Macro: ParaSpaceBefInc6pts, Recorded on 07/21/04
'
' Assigned to Ctrl+Shift+} 'Parallels Ctrl+]
'
' Increase the paragraph Space Before setting by 6 pts (+6).
' See also ParaSpaceBefDec6pts
'=====================================================================
Sub ParaSpaceBefInc6pts()
Const sMacName As String = "ParaSpaceBefInc6pts"
Call ParaSpaceBef6pts("+6", sMacName)
End Sub


'=====================================================================
' Macro: ParaSpaceBef6pts, Recorded on 07/21/04
'
' Called by ParaSpaceBefInc6pts & ParaSpaceBefDec6pts
'
' Syntax: Call ParaSpaceBef6pts(parm, macname)
'
' parm "+" = Increase the paragraph Space Before setting by 6 pts.
' "-" = Decrease the paragraph Space Before setting by 6 pts.
' macname = Name of caller
'
' If it's set to 'Auto' or if the selection is not all the same,
' ask for instructions.
'=====================================================================
Sub ParaSpaceBef6pts(sParm As String, sMacName As String)
Dim nSpBefOld As Single, nSpBefNew As Single 'Settings, old and new
Dim lAmt As Long 'Inc/dec amount (+6/-6)
Dim lBase As Long 'Base amount (6/0)
Dim lReply As Long, sMsg As String

If sParm = "+6" Then 'Check the input parms: "+6" or "-6"
lAmt = 6: lBase = 6
ElseIf sParm = "-6" Then
lAmt = -6: lBase = 0
Else
sMsg = "Logic Error:" & vbCrLf _
& "Invalid parameter (" & sParm & ") from " & sMacName & "." &
vbCrLf _
& "Operation aborted."
Call MsgBox(sMsg, , "ParaSpaceBef6pts")
Exit Sub
End If

'Display the settings (debug). Comment out when not needed.
'sMsg = "Selection.ParagraphFormat.SpaceBefore = " &
Selection.ParagraphFormat.SpaceBefore & vbCrLf _
' & "Selection.ParagraphFormat.SpaceBeforeAuto = " &
Selection.ParagraphFormat.SpaceBeforeAuto
'Call MsgBox(sMsg, , sMacName)
'Exit Sub

nSpBefOld = Selection.ParagraphFormat.SpaceBefore

'If there are different settings (9999999), ask for instructions
'This must be done before the 'Auto' check.
If nSpBefOld > 9999 Then
sMsg = "The selection contains multiple paragraph Space Before
settings. " _
& "Set them all to " & lBase & "?"
lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
If lReply = vbYes Then 'If they say yes, set them all to 0/6
nSpBefNew = lBase
GoTo SetEm
Else 'Otherwise, do nothing
Exit Sub
End If
End If

'If the setting is "Auto" (-1), ask for instructions
If Selection.ParagraphFormat.SpaceBeforeAuto = -1 Then
sMsg = "The current paragraph Space Before setting is 'Auto'. " _
& "Set it to " & lBase & "?"
lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
If lReply = vbYes Then 'If they say yes, set them all to 0 (&
reset 'Auto')
nSpBefNew = lBase
GoTo SetEm
Else 'Otherwise, do nothing
Exit Sub
End If
End If

'If the setting < 6, ask for instructions (Decrement only)
'(Probably OK to just set it to 0)
If sParm = "-6" Then
If nSpBefOld < 6 Then
sMsg = "The current paragraph Space Before setting (" & nSpBefOld &
") is < 6. " _
& "Set it to 0?"
lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
If lReply = vbYes Then 'If they say yes, set them all to 0
nSpBefNew = 0
GoTo SetEm
Else 'Otherwise, do nothing
Exit Sub
End If
End If
End If

'All special conditions eliminated. Do the decrement.
nSpBefNew = nSpBefOld + lAmt

'Update the settings and exit
SetEm:
Selection.ParagraphFormat.SpaceBeforeAuto = 0 'Reset 'Auto',
just in case
Selection.ParagraphFormat.SpaceBefore = nSpBefNew 'Decrement by 6

End Sub
 
T

TF

This is way beyond New Users. Would you post this in the Word VBA Newsgroup
where the VBA gurus lurk.

--
Terry Farrell - Word MVP
http://word.mvps.org/

: I want to thank everyone who helped me get this little macro working.
: I am posting the completed macro in case it might be useful to anyone.
: I would be interested in any comments on the coding or logic.
:
: If it is not appropriate to post code, please advise and I won't do it
: again.
:
: Thanks
:
:
:
: '=====================================================================
: ' Macro: ParaSpaceBefDec6pts, Recorded on 07/21/04
: '
: ' Assigned to Ctrl+Shift+{ 'Parallels Ctrl+[
: '
: ' Decrease the paragraph Space Before setting by 6 pts (-6).
: ' See also ParaSpaceBefInc6pts
: '=====================================================================
: Sub ParaSpaceBefDec6pts()
: Const sMacName As String = "ParaSpaceBefDec6pts"
: Call ParaSpaceBef6pts("-6", sMacName)
: End Sub
:
:
: '=====================================================================
: ' Macro: ParaSpaceBefInc6pts, Recorded on 07/21/04
: '
: ' Assigned to Ctrl+Shift+} 'Parallels Ctrl+]
: '
: ' Increase the paragraph Space Before setting by 6 pts (+6).
: ' See also ParaSpaceBefDec6pts
: '=====================================================================
: Sub ParaSpaceBefInc6pts()
: Const sMacName As String = "ParaSpaceBefInc6pts"
: Call ParaSpaceBef6pts("+6", sMacName)
: End Sub
:
:
: '=====================================================================
: ' Macro: ParaSpaceBef6pts, Recorded on 07/21/04
: '
: ' Called by ParaSpaceBefInc6pts & ParaSpaceBefDec6pts
: '
: ' Syntax: Call ParaSpaceBef6pts(parm, macname)
: '
: ' parm "+" = Increase the paragraph Space Before setting by 6 pts.
: ' "-" = Decrease the paragraph Space Before setting by 6 pts.
: ' macname = Name of caller
: '
: ' If it's set to 'Auto' or if the selection is not all the same,
: ' ask for instructions.
: '=====================================================================
: Sub ParaSpaceBef6pts(sParm As String, sMacName As String)
: Dim nSpBefOld As Single, nSpBefNew As Single 'Settings, old and new
: Dim lAmt As Long 'Inc/dec amount (+6/-6)
: Dim lBase As Long 'Base amount (6/0)
: Dim lReply As Long, sMsg As String
:
: If sParm = "+6" Then 'Check the input parms: "+6" or "-6"
: lAmt = 6: lBase = 6
: ElseIf sParm = "-6" Then
: lAmt = -6: lBase = 0
: Else
: sMsg = "Logic Error:" & vbCrLf _
: & "Invalid parameter (" & sParm & ") from " & sMacName & "." &
: vbCrLf _
: & "Operation aborted."
: Call MsgBox(sMsg, , "ParaSpaceBef6pts")
: Exit Sub
: End If
:
: 'Display the settings (debug). Comment out when not needed.
: 'sMsg = "Selection.ParagraphFormat.SpaceBefore = " &
: Selection.ParagraphFormat.SpaceBefore & vbCrLf _
: ' & "Selection.ParagraphFormat.SpaceBeforeAuto = " &
: Selection.ParagraphFormat.SpaceBeforeAuto
: 'Call MsgBox(sMsg, , sMacName)
: 'Exit Sub
:
: nSpBefOld = Selection.ParagraphFormat.SpaceBefore
:
: 'If there are different settings (9999999), ask for instructions
: 'This must be done before the 'Auto' check.
: If nSpBefOld > 9999 Then
: sMsg = "The selection contains multiple paragraph Space Before
: settings. " _
: & "Set them all to " & lBase & "?"
: lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
: If lReply = vbYes Then 'If they say yes, set them all to 0/6
: nSpBefNew = lBase
: GoTo SetEm
: Else 'Otherwise, do nothing
: Exit Sub
: End If
: End If
:
: 'If the setting is "Auto" (-1), ask for instructions
: If Selection.ParagraphFormat.SpaceBeforeAuto = -1 Then
: sMsg = "The current paragraph Space Before setting is 'Auto'. " _
: & "Set it to " & lBase & "?"
: lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
: If lReply = vbYes Then 'If they say yes, set them all to 0 (&
: reset 'Auto')
: nSpBefNew = lBase
: GoTo SetEm
: Else 'Otherwise, do nothing
: Exit Sub
: End If
: End If
:
: 'If the setting < 6, ask for instructions (Decrement only)
: '(Probably OK to just set it to 0)
: If sParm = "-6" Then
: If nSpBefOld < 6 Then
: sMsg = "The current paragraph Space Before setting (" & nSpBefOld &
: ") is < 6. " _
: & "Set it to 0?"
: lReply = MsgBox(sMsg, vbYesNoCancel + vbDefaultButton2, sMacName)
: If lReply = vbYes Then 'If they say yes, set them all to 0
: nSpBefNew = 0
: GoTo SetEm
: Else 'Otherwise, do nothing
: Exit Sub
: End If
: End If
: End If
:
: 'All special conditions eliminated. Do the decrement.
: nSpBefNew = nSpBefOld + lAmt
:
: 'Update the settings and exit
: SetEm:
: Selection.ParagraphFormat.SpaceBeforeAuto = 0 'Reset 'Auto',
: just in case
: Selection.ParagraphFormat.SpaceBefore = nSpBefNew 'Decrement by 6
:
: End Sub
:
:
: --
: Running Word 2K in Office 2K on Win2K-SR2
: For email, use Usenet-20031220 at spamex.com
 
L

Larry

Is this macro for changing the spacing of a paragraph, or for changing
the direction of the earth's orbit? It looks complicated enough for the
latter.

I remember the original four-line toggle macro I gave you for this, way
back at the beginning.

Larry
 
T

Top Spin

This is way beyond New Users. Would you post this in the Word VBA Newsgroup
where the VBA gurus lurk.

Sure, no problem. I got the help here, so I thought I'd post the
result here.

Thanks
 
T

Top Spin

Is this macro for changing the spacing of a paragraph, or for changing
the direction of the earth's orbit? It looks complicated enough for the
latter.

Perhaps, but it won't break under any of the special circumstances I
am aware of. I'm sure you are well aware that the error handling is
often 10x as much code as the actual funcion.
I remember the original four-line toggle macro I gave you for this, way
back at the beginning.

Naw, that was for a simple toggle. I still have that one, but it, too,
has grown a bit.

See what you started?
 
L

Larry

I haven't followed all the development of it, except that it kept
becoming more and more complicated. Could you tell me what it does?

Larry
 
T

Top Spin

I haven't followed all the development of it, except that it kept
becoming more and more complicated. Could you tell me what it does?

Sure. I guess my attempt to make it self-documenting was not that
successful! ;-)

There are actually 3 macros there: 2 front ends and one backend that
does all the work.

One macro increments the paragraph "Space Before" setting by 6 points
and the other decrements it by 6 points. I have them assigned to the
shortcut keys Ctrl+{ and Crtl+} because they have similar actions to
Ctrl+[ and Ctrl+] for font size.

I initially had two separate macros:

ParaSpaceBefInc6pts would increase the spacing.

ParaSpaceBefDec6pts would decrease it.

As they got more and more complicated, I got tired of making the
changes in two places, so I put the guts of the code in a back end
macro, which I named "ParaSpaceBef6pts". The original macros now
simply process the shortcut keystroke and pass a parameter to the
backend routine.

The backend code does all sorts of checking to avoid errors and then
does the increment or decrement. It uses MsgBox to get assistamce from
the user if necessary.

How's that? It might not be ready for a shareware release, but I use
it all the time -- along with the original toggle you helped me with.

Cheers
 
L

Larry

Ok, but remember that macros should not have unnecessary complications.
You could achieve the same tasks with the following macros. Errors are
handled. If you want a message box, you could add that. In my opinion,
the macros you created are far too complicated for the simple task you
have set yourself here.

Also, just a a personal opinion, for such a simple task, I wouldn't
think a message box was needed. It would be like having a message box
for, say, the EditCopy command: "Do you want to copy the selected
text"? I would reserve message boxes for macros that make more
significant changes in a document, that you may want to get a warning
about before you do it, not for such a simple task as changing the
SpaceBefore property of a paragraph. Anyway, those are my thoughts.

Larry

Sub IncreaseSpaceBefore()
On Error GoTo Goodby
Dim sb As Long
sb = Selection.ParagraphFormat.SpaceBefore
Selection.ParagraphFormat.SpaceBefore = sb + 6
Goodby:
Err.Clear
End Sub

Sub DecreaseSpaceBefore
Dim sb As Long
sb = Selection.ParagraphFormat.SpaceBefore
On Error GoTo Goodby
Selection.ParagraphFormat.SpaceBefore = sb - 6
Goodby:
Err.Clear
End Sub



Top Spin said:
I haven't followed all the development of it, except that it kept
becoming more and more complicated. Could you tell me what it does?

Sure. I guess my attempt to make it self-documenting was not that
successful! ;-)

There are actually 3 macros there: 2 front ends and one backend that
does all the work.

One macro increments the paragraph "Space Before" setting by 6 points
and the other decrements it by 6 points. I have them assigned to the
shortcut keys Ctrl+{ and Crtl+} because they have similar actions to
Ctrl+[ and Ctrl+] for font size.

I initially had two separate macros:

ParaSpaceBefInc6pts would increase the spacing.

ParaSpaceBefDec6pts would decrease it.

As they got more and more complicated, I got tired of making the
changes in two places, so I put the guts of the code in a back end
macro, which I named "ParaSpaceBef6pts". The original macros now
simply process the shortcut keystroke and pass a parameter to the
backend routine.

The backend code does all sorts of checking to avoid errors and then
does the increment or decrement. It uses MsgBox to get assistamce from
the user if necessary.

How's that? It might not be ready for a shareware release, but I use
it all the time -- along with the original toggle you helped me with.

Cheers
 
L

Larry

For the increase macro, there would be no error, the space could just
keep increasing, so I suppose you could set an upper limit for the
space, like this:

Dim sb As Long
sb = Selection.ParagraphFormat.SpaceBefore
If sb > 18 Then Exit Sub
Selection.ParagraphFormat.SpaceBefore = sb + 6

Larry
 
T

Top Spin

Ok, but remember that macros should not have unnecessary complications.

I guess the definition of "unnecessary" is somewhat subjective
You could achieve the same tasks with the following macros. Errors are
handled. If you want a message box, you could add that.

Your macros do not provide the same function. Errors are not
"handled", they are ignored. I regularly select multiple lines of text
and invoke the macros. Sometimes those lines have different spacing.
My macro allows me the option of aborting (in case I selected multiple
lines by mistake) or proceeding it what I wanted, as I often do, is to
change them all to 0 or 6.
In my opinion,
the macros you created are far too complicated for the simple task you
have set yourself here.

I suppose if I had contracted to write it for you, it is too
complicated. For my needs, it does just what I need it to do.
Also, just a a personal opinion, for such a simple task, I wouldn't
think a message box was needed. It would be like having a message box
for, say, the EditCopy command: "Do you want to copy the selected
text"?

The message boxes do not simply ask the user if they want to do what
they just asked the macro to do. The message boxes only appear if
there is some anomoly.
I would reserve message boxes for macros that make more
significant changes in a document, that you may want to get a warning
about before you do it, not for such a simple task as changing the
SpaceBefore property of a paragraph. Anyway, those are my thoughts.

Thanks. I appreciate comments.
 
L

Larry

Ok, I guess that you have much more functionality there than I realized
and that you need all that code. However, the simple macro I sent you
will make the change on multiple paragraphs. Also, I don't know much
about errors, but whether the error is handled or just ignored, the
point is that with my code an error message is avoided.

Larry
 

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