Set ParagraphFormat.SpaceAfter based on the style of the previous paragraph

P

penfield888

Not sure where to start on this and would appreciate any help anyone
can offer. I've searched the archives a fair bit, but haven't seen
anyone doing this.

I want to set the Spacing Before to be the rough equivalent of double
spacing before a particular style ("Case Heading") EXCEPT when preceded
by the style "FolioNumSepLine". In that case I want to suppress the
spacing before.

So in pseudocode, what I want to do is

if ( Not first paragraph in document
AND CurrentParagraph.Style is "Case Heading"
AND PrecedingParagraph.Style is "FolioNumSepLine"
)

set CurrentParagraph.SpaceBefore to 0

elseif ( Not first paragraph in document
AND CurrentParagraph.Style is "Case Heading"
AND PrecedingParagraph.Style is NOT "FolioNumSepLine"
)

set CurrentParagraph.SpaceBefore to 0

endif


For background, the situation is this. I'm doing an edition of a text
and must indicate where page breaks take place in the original text.
If the manuscript page number is immediately above the heading, I want
it there without extra spacing. Otherwise, I want a double space above
the heading. This is the format for the series in which the volume
appears, so it is what it is and I can't change that. So the situations
would be:

#1 - heading in middle of manuscript page, ms page number in middle of
a line.

text text text text text text text [f. 231] text text text text text
text text text text text text

heading heading heading heading heading heading

-----------------

#2 - heading at top of manuscript page.
text text text text text text text text text text text text text text
text text text text

[f. 231]
heading heading heading heading heading heading
 
J

Jay Freedman

Not sure where to start on this and would appreciate any help anyone
can offer. I've searched the archives a fair bit, but haven't seen
anyone doing this.

I want to set the Spacing Before to be the rough equivalent of double
spacing before a particular style ("Case Heading") EXCEPT when preceded
by the style "FolioNumSepLine". In that case I want to suppress the
spacing before.

So in pseudocode, what I want to do is

if ( Not first paragraph in document
AND CurrentParagraph.Style is "Case Heading"
AND PrecedingParagraph.Style is "FolioNumSepLine"
)

set CurrentParagraph.SpaceBefore to 0

elseif ( Not first paragraph in document
AND CurrentParagraph.Style is "Case Heading"
AND PrecedingParagraph.Style is NOT "FolioNumSepLine"
)

set CurrentParagraph.SpaceBefore to 0

endif


For background, the situation is this. I'm doing an edition of a text
and must indicate where page breaks take place in the original text.
If the manuscript page number is immediately above the heading, I want
it there without extra spacing. Otherwise, I want a double space above
the heading. This is the format for the series in which the volume
appears, so it is what it is and I can't change that. So the situations
would be:

#1 - heading in middle of manuscript page, ms page number in middle of
a line.

text text text text text text text [f. 231] text text text text text
text text text text text text

heading heading heading heading heading heading

-----------------

#2 - heading at top of manuscript page.
text text text text text text text text text text text text text text
text text text text

[f. 231]
heading heading heading heading heading heading

The recommended method is to use the .Find method of a Range object to
search for text formatted in Case Heading style. Each time you find
one, you create a second Range object, set it to point to the
paragraph before the one you found, and check its style. I hope the
comments in the following code are enough to get you going. (It
assumes that all the text has the wide Space Before to start with,
presumably because it's part of the style definitions, and just
removes the space when it finds the specified pair of styles
together.)

Sub AdjustSpaceBefore()
Dim oRg As Range, oPrevParaRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
' set up Find to look for Case Heading style
.ClearFormatting
.Style = "Case Heading"
.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = False

' each time Case Heading is found, the .Execute
' method returns True and the Do...Loop runs;
' when there are no more to find, .Execute
' returns False and the loop stops
Do While .Execute
' create an independent range to
' look at the preceding paragraph
' (note: if oRg includes the first paragraph
' of the document, the .Move and .MoveStartUntil
' won't go anywhere, so oPrevParaRg will have
' the same Case Heading style
Set oPrevParaRg = oRg.Duplicate
oPrevParaRg.Move wdCharacter, -2
oPrevParaRg.MoveStartUntil vbCr, wdBackward

' check its style
If oPrevParaRg.Style = "FolioNumSepLine" Then
' adjust the spacing
oRg.Paragraphs(1).SpaceBefore = 0
End If

' move the search range so the same text
' isn't found again
oRg.Collapse wdCollapseEnd
Loop
End With

End Sub
 
P

penfield888

Thanks! That points me in the right direction and gives me the info I
need.

I'll modify slightly so that I can delete the double returns put in by
a previous editor and do this check at the same time.

Basically, in terms of final effect, this boils down to the same thing
setting a custom SpaceBefore on some Case Heading paragraphs, right?
Since I would be setting it to zero, I guess that's fine.

It's too bad that Word can't do this more dynamically. All I would
really need is to set a negative SpaceAfter on the FolioNumSepLine
style equal to the SpaceBefore on the CaseHeading style and that would
handle it, but Word doesn't seem to allow negative SpaceAfter (at least
not via the normal applicaiton interface, which makes me assume that
either nothing or bad things would happen if I set it to a negative
number wth VBA)


Anyway, thanks so much for your time and effort. Much appreciated!
 

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