Macros to change Line and Paragraph spacing

M

Moishy

Hello all Experts,

How can I change line and paragraph spacing using vba?

Using VBA I would like to create several macros.
1. to increase line spacing of selected text by the smallest possible
amount.
2. the same to decrease line spacing.
3. to increase paragraph spacing of selected paragraphs
4. same to decrease paragraph spacing.

---
 
S

Stefan Blom

Try something like this:

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

A similar couple of macros can easily be created for Spacing After. I have
set the increment to 1 point but you can change that if you want to.

As far as the line spacing is concerned, you will have to decide if you want
variable or fixed line spacing, before starting to code.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Moishy" wrote in message

Hello all Experts,

How can I change line and paragraph spacing using vba?

Using VBA I would like to create several macros.
1. to increase line spacing of selected text by the smallest possible
amount.
2. the same to decrease line spacing.
3. to increase paragraph spacing of selected paragraphs
4. same to decrease paragraph spacing.

---
 
M

Moishy

Try something like this:

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

A similar couple of macros can easily be created for Spacing After. I have
set the increment to 1 point but you can change that if you want to.

As far as the line spacing is concerned, you will have to decide if you want
variable or fixed line spacing, before starting to code.

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"Moishy"  wrote in message


Hello all Experts,

How can I change line and paragraph spacing using vba?

Using VBA I would like to create several macros.
1. to increase line spacing of selected text by the smallest possible
amount.
2. the same to decrease line spacing.
3. to increase paragraph spacing of selected paragraphs
4. same to decrease paragraph spacing.

---

Stefan,

Thank you for your help. I have a couple of questions:

1. I get a compile error: Invalid use of property when I run the
macro you posted.
2. Is 1 point the smallest possible unit or can it be 0.5 or
smaller?
3. What did you mean when you wrote: 'you will have to decide if
you want variable or fixed line spacing'? If you meant the
LineSpacingRule, it should be wdLineSpaceExactly (if not already) at
double the points of the size of the font.
 
S

Stefan Blom

I apologize. I see that I forgot an equals sign (assignment operator):

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

You should be able to use increments of 0.1 points, if you want that.
However, I doubt that you need so small values...

OK, you are using a fixed value for line spacing. How do you want it to be
changed then?

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Moishy" wrote in message

Try something like this:

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

A similar couple of macros can easily be created for Spacing After. I have
set the increment to 1 point but you can change that if you want to.

As far as the line spacing is concerned, you will have to decide if you
want
variable or fixed line spacing, before starting to code.

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"Moishy" wrote in message


Hello all Experts,

How can I change line and paragraph spacing using vba?

Using VBA I would like to create several macros.
1. to increase line spacing of selected text by the smallest possible
amount.
2. the same to decrease line spacing.
3. to increase paragraph spacing of selected paragraphs
4. same to decrease paragraph spacing.

---

Stefan,

Thank you for your help. I have a couple of questions:

1. I get a compile error: Invalid use of property when I run the
macro you posted.
2. Is 1 point the smallest possible unit or can it be 0.5 or
smaller?
3. What did you mean when you wrote: 'you will have to decide if
you want variable or fixed line spacing'? If you meant the
LineSpacingRule, it should be wdLineSpaceExactly (if not already) at
double the points of the size of the font.
 
M

Moishy

I apologize. I see that I forgot an equals sign (assignment operator):

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

You should be able to use increments of 0.1 points, if you want that.
However, I doubt that you need so small values...

OK, you are using a fixed value for line spacing. How do you want it to be
changed then?

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"Moishy"  wrote in message













Stefan,

Thank you for your help. I have a couple of questions:

    1. I get a compile error: Invalid use of property when I run the
macro you posted.
    2. Is 1 point the smallest possible unit or can it be 0.5 or
smaller?
    3. What did you mean when you wrote: 'you will have to decide if
you want variable or fixed line spacing'? If you meant the
LineSpacingRule, it should be wdLineSpaceExactly (if not already) at
double the points of the size of the font.

Thanks for the correction.
You wrote: 'How do you want it to be changed then?' sorry I didn't
understand the question.
 
S

Stefan Blom

Well, here is a suggestion for setting the line spacing:

Sub SetFixedLineSpacingInPara()
With Selection.ParagraphFormat
..LineSpacingRule = wdLineSpaceExactly
..LineSpacing = 2 * Selection.Characters(1).Font.Size
End With
End Sub

In practice, the font size can vary within a paragraph. You can avoid this
possible issue by resetting the font properties for all text in the
selection or by looping all characters to find out which is the largest font
present in the selection.

You may also want to take a look at macropod's (Paul Edstein) reply in the
Word Developer forum at MSDN. :)

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Moishy" wrote in message

I apologize. I see that I forgot an equals sign (assignment operator):

Sub IncreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore + 1
Exit Sub
errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

Sub DecreaseSpacingBefore()
On Error GoTo errhandler
Selection.ParagraphFormat.SpaceBefore = _
Selection.ParagraphFormat.SpaceBefore - 1
Exit Sub

errhandler:
Selection.ParagraphFormat.SpaceBefore = 6 'or the value you prefer
End Sub

You should be able to use increments of 0.1 points, if you want that.
However, I doubt that you need so small values...

OK, you are using a fixed value for line spacing. How do you want it to be
changed then?

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"Moishy" wrote in message













Stefan,

Thank you for your help. I have a couple of questions:

1. I get a compile error: Invalid use of property when I run the
macro you posted.
2. Is 1 point the smallest possible unit or can it be 0.5 or
smaller?
3. What did you mean when you wrote: 'you will have to decide if
you want variable or fixed line spacing'? If you meant the
LineSpacingRule, it should be wdLineSpaceExactly (if not already) at
double the points of the size of the font.

Thanks for the correction.
You wrote: 'How do you want it to be changed then?' sorry I didn't
understand the question.
 
M

Moishy

Well, here is a suggestion for setting the line spacing:

Sub SetFixedLineSpacingInPara()
With Selection.ParagraphFormat
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 2 * Selection.Characters(1).Font.Size
End With
End Sub

In practice, the font size can vary within a paragraph. You can avoid this
possible issue by resetting the font properties for all text in the
selection or by looping all characters to find out which is the largest font
present in the selection.

You may also want to take a look at macropod's (Paul Edstein) reply in the
Word Developer forum at MSDN. :)

--
Stefan Blom
Microsoft Word MVP

---------------------------------------------"Moishy"  wrote in message











Thanks for the correction.You wrote: 'How do you want it to be changed then?' sorry I didn't

understand the question.

Thank you:)
 

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