PDA

View Full Version : Solved: Increase line spacing of last paragraph



Dave T
12-12-2010, 11:46 PM
Hello All,

I have created a style within Word 2003 and am wondering if it is possible for a macro to increase the line spacing of the last line.

I am hoping it is possible to select a range of paragraphs and when the style is applied the very last paragraph has a larger 'space after' the style is applied.

I found a macro that will do this for List Numbers:


Sub ListNumber()
'Macro created by John McGhie
With Selection.Paragraphs
.Style = "List Number"
With .First.Range.ListFormat
.ApplyListTemplate .ListTemplate, False
End With
With .Last
.KeepWithNext = False
.SpaceAfter = 10
End With
End With
End Sub

Not sure if this can be done for a general paragraph style.

Any comments appreciated,

Regards,
Dave T

gmaxey
12-13-2010, 06:28 AM
Try:

Sub ScratchMacro()
With Selection.Paragraphs
.Style = "Normal" 'Whatever style you want to apply
.Last.SpaceAfter = .SpaceAfter + 10 'Whatever additional space you want to add
End With
End Sub

fumei
12-13-2010, 09:56 AM
Bleech.

Dave T
12-13-2010, 03:42 PM
Thanks for the reply Greg,

Must admit I had a chuckle at Gerry's response, but i was just curious if I could avoid the need to create another style called "Macro Text Last" based on a style called "Macro Text" that contained the extra paragraph spacing.
To apply the styles I would need to apply the "Macro Text" style to the selected text and then select the last paragraph and apply the "Macro Text Last".
If I just use the one style it is not possible to add the extra line spacing to the last paragraph and was curious if this copuld all be done in one action using VBA.

I modified some code from Greg's site (http://gregmaxey.mvps.org/Margin_Text.htm) to create a style I have called "Macro Text"


Sub MacroText()
Dim styleName As String
Dim oStyle As Style
styleName = "Macro"
'Create/Setup the style
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styleName Then GoTo Setup
Next oStyle
ActiveDocument.Styles.Add Name:=styleName, Type:=wdStyleTypeParagraph
Setup:
With ActiveDocument.Styles(styleName)
.AutomaticallyUpdate = False
.BaseStyle = ""
.NextParagraphStyle = "Macro"
End With
With ActiveDocument.Styles(styleName).Font
.Name = "Courier New"
.Size = 9
.ColorIndex = wdBlue
End With
With ActiveDocument.Styles(styleName).ParagraphFormat
.LineSpacingRule = wdLineSpaceSingle
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.OutlineLevel = wdOutlineLevelBodyText
.Shading.BackgroundPatternColor = wdColorGray10
End With
End Sub


I could create a new style base on the "Macro Text" style or I could use another macro just to add the extra paragraph when the last paragraph has been selected.
Using either:


Sub SpaceAfter6()
'Puts 6 points of space after the paragraph
Selection.ParagraphFormat.SpaceAfter = 6
End Sub

OR


Sub SpaceAfter10()
'Adds ten points of space after each time you click
Selection.ParagraphFormat.SpaceAfter = Selection.ParagraphFormat.SpaceAfter + 10
End Sub

I was curious if both of these actions could be achieved in one macro.

Regards,
Dave T

fumei
12-15-2010, 09:45 AM
What is the problem with having an explicit style for the last paragraph? I do this for bulleted lists.

The items are Bullet1. The last item is Bullet1_End. I keep the spacing tighter IN the list, but this does not look good for the final item. It needs more space. So...it gets a style.

"I was curious if both of these actions could be achieved in one macro."

Which "both"? You have three examples of code there. Plus I am not quite following. Are you selecting a bunch of text, or ONLY the last paragraph?

"I would need to apply the "Macro Text" style to the selected text and then select the last paragraph "

"when the last paragraph has been selected."

Which is it?

If you want to apply one style to all paragraph except the last one in a Slection:
For Each oPara In Selection.Paragraphs
oPara.Range.Style = "yadda"
Next
Selection.Paragraphs(Selection.Paragraphs.Count).Range.Style = "yadda_END"
True this applies "yadda" to ALL of them first, then applies "yadda_END" to the last one.

There are a multitude of ways to do this.

Dave T
12-19-2010, 10:13 PM
Hello Gerry,

I have always set up another 'last' style based on the previous style for where I wanted extra line spacing at the end of a paragraph (e.g. ‘List Bullet Last’, ‘List Number Last’, etc.). Doing this usually meant applying one style to the required paragraphs and then apply another style to the last paragraph… a two step process.

The first post was the code to set up the base style and the last two examples were one of the methods I use/d to manually apply formatting to the last paragraph.

I knew that a style could not add the extra line spacing at the last paragraph, but thought it might be able to be done via VBA. I must admit I hadn’t considered your simple approach of applying both styles, which works extremely well.

When I was looking at ways of doing this I found another more complicated version which also requires several styles to be set up (http://www.vb-helper.com/howto_format_code_in_word.html)

By the way I have never heard of the term ‘Bleech’ and I did a search for it on Google and this is what I came up with http://www.urbandictionary.com/define.php?term=bleech

Thanks again for you help and suggestions.

Regards,
Dave T

fumei
12-20-2010, 11:25 AM
Well that site is blocked by our firewall, so I do not know what it stated "bleech" meant. Something rude? It is not rude in my mind; it is similar to "yuck". Sort of mildly unpleasant.

gmaxey
12-20-2010, 01:01 PM
Two factors here: 1) Meaning and 2) Intent

Based on a rather loose meaning one could infer an insult. However I don't think an insult was intended or implied.

Now I know that I was right ;-)

fumei
12-20-2010, 01:07 PM
Oh good heavens no! No insult intended.

I simply find ANY manual formatting a teech distasteful.

I'm kidding! I'm kidding!

Dave T
12-22-2010, 06:10 PM
Hello Gerry & Greg,

I never took any offence at the comment.
Just had a chuckle at what it could mean.

All is good...

Enjoy the festive season.

Regards,
Dave T

Paul_Hossler
12-23-2010, 09:49 AM
Bleech.
Gerry
Microsoft Word MVP
<60 working days until retirement


Less than 60 days? Now we can count on seeing you here a lot more

:beerchug:

Paul

fumei
12-23-2010, 12:25 PM
Nope, actually the opposite. I do not have Internet access at home (been Internet-free for more than two years now). So once I leave work, I will be off-line for good.

Oh I may have the opportunity to get on-line once in a while, but for the most part, when I retire my on-line life will be over as well.

Dave T
12-23-2010, 03:07 PM
I am sure we will all miss you Gerry.
Your help to me and other has been fantastic.
All the best with your retirement.

Regards,
Dave T