locked
Macros to change Line and Paragraph spacing RRS feed

  • Question

  • 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.

    ---
    Tuesday, February 14, 2012 2:02 PM

Answers

  • "moishy" wrote in message news:581f9774-00f3-41d3-a6db-8ceee96efcfd@communitybridge.codeplex.com...
    Thank you Mike for giving attention for my question.
    I'm aware that I can record a macro, but there are at least two limitations I can think of that will render that option pointless.
    a. I can't record a macro to adjust the paragraph spacing (i.e. nothing is recorded).
    b. Recording a macro for the line spacing, I will have to hard code the change (for example: Selection.ParagraphFormat.LineSpacing LinesToPoints(1.15)). I need it to increase or decrease by the smallest possible option every time the macro is ran.
         

       

    For what you want to do, the macro recorder is definitely not the way to go. For Spacing Before, you’ll need something along these lines:

    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



    • Edited by Stefan BlomMVP Tuesday, February 14, 2012 7:42 PM (typo)
    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 6:26 PM
  • "moishy" wrote in message news:6d3bb4ea-c6df-4f2a-82a8-ccb4a45a53b1@communitybridge.codeplex.com...
    I'm not sure what you mean. I would like the macro to detect the font size and set the spacing to double that value.
     
     
    OK, that’s simple, at least in theory. You could look at the font size of the first character, say, in the paragraph and use that as a starting point:
     
    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.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 10:40 PM
  • Hi Moishy,

    As a general rule, changes to paragraph attributes should be done by changing the underlying Styles, not be direct formatting. However, as it seems you may be doing this to manage the page composition, perhaps there's a valid reason in this case.

    Below are some macros to adjust both the line spacing and paragraph-before spacing, by 0.5pt increments. The same kind of thing can be done with the paragraph-after spacing I've also tossed in a couple more you might like to play with, for adjusting the character spacing and scaling.

    Sub IncreaseParaSpaceBefore()
    On Error Resume Next
    With Selection.ParagraphFormat
      .SpaceBefore = .SpaceBefore + 0.5
    End With
    End Sub

    Sub DecreaseParaSpaceBefore()
    On Error Resume Next
    With Selection.ParagraphFormat
      .SpaceBefore = .SpaceBefore - 0.5
    End With
    End Sub

    Sub IncreaseLineSpace()
    On Error Resume Next
    With Selection.ParagraphFormat
      .LineSpacing = .LineSpacing + 0.5
    End With
    End Sub

    Sub DecreaseLineSpace()
    On Error Resume Next
    With Selection.ParagraphFormat
      .LineSpacing = .LineSpacing - 0.5
    End With
    End Sub

    Sub IncreaseCharSpace()
    On Error Resume Next
    With Selection.Font
      .Spacing = .Spacing + 0.05
    End With
    End Sub

    Sub DecreaseCharSpace()
    On Error Resume Next
    With Selection.Font
      .Spacing = .Spacing - 0.05
    End With
    End Sub

    Sub IncreaseCharScale()
    On Error Resume Next
    With Selection.Font
      .Scaling = .Scaling - 1
    End With
    End Sub

    Sub DecreaseCharScale()
    On Error Resume Next
    With Selection.Font
      .Scaling = .Scaling - 1
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 10:49 PM

All replies

  • Hi Moishy,

    I think the easiest way for you to handle this is to record some macros.  From the View tab in MS Word (2007 and 2010), in the Macros group click the Record Macro button. You will be presented with a dialog.  Enter a descriptive name for the Macro.  If you want to have this macro available for use in any open document, make sure to save the macro in the Normal.dotm file. This file is opened each time Word starts up. You can even assign keyboard shortcuts to your macros. If you are using Word 2003 or below, look in the Tools menu under Macros.

    Once you get through this dialog the macro recorder will record everything you do. Simply select your text, change line spacing or whatever you need to do.  Once you are done, go back to the View tab and select Stop Recording from the Macro group.

    To run you macros on any open document, simple select your text, then go to the View tab and click the Macros button. You will see a list of all available macros.  Choose the one you need and run it.

    Please mark this post as an answer if you found it helpful and do let me know if you have more questions.

    Thanks,

    Mike Corkery, MCT, MCPD, MCITP, MSF, etc.

    Tuesday, February 14, 2012 3:52 PM
  • Thank you Mike for giving attention for my question.

    I'm aware that I can record a macro, but there are at least two limitations I can think of that will render that option pointless.

    a. I can't record a macro to adjust the paragraph spacing (i.e. nothing is recorded).

    b. Recording a macro for the line spacing, I will have to hard code the change (for example: Selection.ParagraphFormat.LineSpacing = LinesToPoints(1.15)). I need it to increase or decrease by the smallest possible option every time the macro is ran.

    Tuesday, February 14, 2012 6:16 PM
  • "moishy" wrote in message news:581f9774-00f3-41d3-a6db-8ceee96efcfd@communitybridge.codeplex.com...
    Thank you Mike for giving attention for my question.
    I'm aware that I can record a macro, but there are at least two limitations I can think of that will render that option pointless.
    a. I can't record a macro to adjust the paragraph spacing (i.e. nothing is recorded).
    b. Recording a macro for the line spacing, I will have to hard code the change (for example: Selection.ParagraphFormat.LineSpacing LinesToPoints(1.15)). I need it to increase or decrease by the smallest possible option every time the macro is ran.
         

       

    For what you want to do, the macro recorder is definitely not the way to go. For Spacing Before, you’ll need something along these lines:

    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



    • Edited by Stefan BlomMVP Tuesday, February 14, 2012 7:42 PM (typo)
    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 6:26 PM
  • 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.
    Tuesday, February 14, 2012 7:31 PM
  • "moishy" wrote in message news:54348a8d-07f9-4300-8989-eb11b9483991@communitybridge.codeplex.com...
    Stefan,
     
    Thank you for your help. I have a couple of questions:
     
    I get a compile error: Invalid use of property when I run the macro you posted.
    Is 1 point the smallest possible unit or can it be 0.5 or smaller?
    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.
     
     
     
    Sorry about the typo in the code; I have now corrected it.
     
    Are you saying that the macro should set a fixed value for line spacing, or should the macro also allow for changes of the line spacing?

    Stefan Blom, Microsoft Word MVP
    Tuesday, February 14, 2012 7:45 PM
  • Thanks for the correction.

    Are you saying that the macro should set a fixed value for line spacing, or should the macro also allow for changes of the line spacing?

    I'm not sure what you mean. I would like the macro to detect the font size and set the spacing to double that value.
    Tuesday, February 14, 2012 8:22 PM
  • "moishy" wrote in message news:6d3bb4ea-c6df-4f2a-82a8-ccb4a45a53b1@communitybridge.codeplex.com...
    I'm not sure what you mean. I would like the macro to detect the font size and set the spacing to double that value.
     
     
    OK, that’s simple, at least in theory. You could look at the font size of the first character, say, in the paragraph and use that as a starting point:
     
    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.

    Stefan Blom, Microsoft Word MVP
    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 10:40 PM
  • Hi Moishy,

    As a general rule, changes to paragraph attributes should be done by changing the underlying Styles, not be direct formatting. However, as it seems you may be doing this to manage the page composition, perhaps there's a valid reason in this case.

    Below are some macros to adjust both the line spacing and paragraph-before spacing, by 0.5pt increments. The same kind of thing can be done with the paragraph-after spacing I've also tossed in a couple more you might like to play with, for adjusting the character spacing and scaling.

    Sub IncreaseParaSpaceBefore()
    On Error Resume Next
    With Selection.ParagraphFormat
      .SpaceBefore = .SpaceBefore + 0.5
    End With
    End Sub

    Sub DecreaseParaSpaceBefore()
    On Error Resume Next
    With Selection.ParagraphFormat
      .SpaceBefore = .SpaceBefore - 0.5
    End With
    End Sub

    Sub IncreaseLineSpace()
    On Error Resume Next
    With Selection.ParagraphFormat
      .LineSpacing = .LineSpacing + 0.5
    End With
    End Sub

    Sub DecreaseLineSpace()
    On Error Resume Next
    With Selection.ParagraphFormat
      .LineSpacing = .LineSpacing - 0.5
    End With
    End Sub

    Sub IncreaseCharSpace()
    On Error Resume Next
    With Selection.Font
      .Spacing = .Spacing + 0.05
    End With
    End Sub

    Sub DecreaseCharSpace()
    On Error Resume Next
    With Selection.Font
      .Spacing = .Spacing - 0.05
    End With
    End Sub

    Sub IncreaseCharScale()
    On Error Resume Next
    With Selection.Font
      .Scaling = .Scaling - 1
    End With
    End Sub

    Sub DecreaseCharScale()
    On Error Resume Next
    With Selection.Font
      .Scaling = .Scaling - 1
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Wednesday, February 15, 2012 7:49 AM
    Tuesday, February 14, 2012 10:49 PM
  • Thank you both you just made my life a whole lot easier.

    Just in case anyone was wondering, the issue I'm dealing with is page layout in multi-column documents, since Word doesn't have a built in option to align all columns equally at the bottom, I have to do it manually, these macros will really come in handy :)

    Wednesday, February 15, 2012 7:55 AM
  • Just in case anyone was wondering, the issue I'm dealing with is page layout in multi-column documents, since Word doesn't have a built in option to align all columns equally at the bottom, I have to do it manually, these macros will really come in handy :)

    Actually, it does, to an extent - simply insert a Continuous Section break at the bottom of the last column on the page. The columns will then typically balance to within a line or so. This might help reduce the amount of balancing you need to do via the macros.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, February 15, 2012 8:23 AM
  • Actually, it does, to an extent - simply insert a Continuous Section break at the bottom of the last column on the page. The columns will then typically balance to within a line or so. This might help reduce the amount of balancing you need to do via the macros.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sorry, forgot to mention that I do that.
    Wednesday, February 15, 2012 10:24 AM
  • Thanks, This code is worked very well for me thanks a lot for suggestion
    Thursday, December 21, 2017 7:34 AM