none
Cannot create macro for removing spaces above & below the paragraph RRS feed

  • Question

  • Hi, 

    I usually keep on copying the web text to word files. These texts are usually copied with spaces above & below the paragraphs. So, I want to automate the task for removing both the spaces (above & below the selected paragraph) through some keystroke

    I use to create a macro, by recording it rather than coding it since I have a  little experience of coding in Word VB (though I am quite a good VB.NET, C# & Java coder)

    So, this is what I did:

    1. I first selected (by cursor) any paragraph that have the spaces above & below it.
    2. I went to Developer tab -> Record Macro (in Code section) -> & then gave name for the macro & assigned an unassigned key-shortcut -> clicked Assign -> Close
    3. This started recording the macro
    4. Then I went to Home tab -> Clicked Line & Paragraph Spacing (in Paragraph section) -> Remove the space above the paragraph -> Remove the space above the paragraph
    5. Went back to Developer tab to stop the macro

    Now when I selected another such paragraph & hit the same keystrokes, nothing happened.

    I went to its code, but it showed that it did not recorded anything:

    Sub RemoveSpaceAboveBelow() ' ' RemoveSpaceAboveBelow Macro ' ' End Sub

    I tried this thrice, but no result.

    So I am sure I am lacking some knowledge here as I am less experienced in Word programming. Please help

     

    One more question:

    Is it possible to backup the macro, so that when I format the PC & reinstall I can reapply them

    Many a times I re-recorded them from scratch. This is in fact a lot time consuming, since I automate substantial amount of doc formatting task using many macros, so it takes a lot time to re-record them all.

    I found the way to backup the auto-correct dictionary, but unable to device the way to backup macros.

    Please help. Thank you.

     

     

     

    Tuesday, April 12, 2011 12:50 PM

Answers

  • Hi Mahesh

    <<One more question:

    Is it possible to backup the macro, so that when I format the PC & reinstall I can reapply them>>

    There are various possibilities, but the one most people use is to back up the copy of the file containing the macro. If you look in the VBA Editor, that would be the "Projects" window. The top level there is the file involved and, when you record a macro, this is going to be Normal.dot(m if you're working in version 2007 or 2010).

    So the "trick" is to find out where Normal.dot(m) is saved on disk and back that up. Given the programming languages you work with, I figure you know how to use Windows well enough to search the file :-)? Just make sure you're seeing "hidden files" and you should turn it up, probably under your user profile, somewhere (depends on the version of Word and of Windows).

    The other way you can back up a macro is as a plain text file. Right-click on the module ("New Macros", for example) in the Project window then choose "Export file". To "re-install" these, you right-click the "Modules" entry and choose "Import file".

    Using this latter approach is never a bad idea, as Word files can "go bad" and result in not being able to access the "macro storage" :-)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mahesha999 Tuesday, April 12, 2011 3:12 PM
    Tuesday, April 12, 2011 2:46 PM
    Moderator
  • The Normal style (& also No Spacing style, that I tried) does many formatting without removing only the spaces above & below the paragraph.

    But thats OK, your comment gave me the idea. By the way it is very easy.

    The code for the macro to remove spaces above & below the selected paragraph is as follows:

     

    Sub RemoveSpaceAboveBelow()

        Selection.ParagraphFormat.SpaceAfter = 0

        Selection.ParagraphFormat.SpaceBefore = 0

    End Sub

     

    That was really easy. It wont change your selection font neither the size.

    Thank you all for your quick responses.


    • Marked as answer by Mahesha999 Tuesday, April 12, 2011 3:18 PM
    Tuesday, April 12, 2011 3:17 PM

All replies

  • Hi Mahesh,

    If there are unwanted spaces between the lines, that suggest you need to modify the format of the paragraph Style the text is being formatted in (probably the 'Normal' Style). Simply change the Style Format. Here's a macro to do just that:

    Sub Reformat()
    With ActiveDocument
      .Content.Style = "Normal"
      With .Styles("Normal")
        .ParagraphFormat.SpaceAfter = 0
        .ParagraphFormat.SpaceBefore = 0
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Font.Size = 10
        .Font.Name = "Arial Narrow"
      End With
    End With
    End Sub

    The macro does more than just remove the before/after spacing, just to show you some of what can be done.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, April 12, 2011 2:00 PM
  • Hi Mahesh

    <<One more question:

    Is it possible to backup the macro, so that when I format the PC & reinstall I can reapply them>>

    There are various possibilities, but the one most people use is to back up the copy of the file containing the macro. If you look in the VBA Editor, that would be the "Projects" window. The top level there is the file involved and, when you record a macro, this is going to be Normal.dot(m if you're working in version 2007 or 2010).

    So the "trick" is to find out where Normal.dot(m) is saved on disk and back that up. Given the programming languages you work with, I figure you know how to use Windows well enough to search the file :-)? Just make sure you're seeing "hidden files" and you should turn it up, probably under your user profile, somewhere (depends on the version of Word and of Windows).

    The other way you can back up a macro is as a plain text file. Right-click on the module ("New Macros", for example) in the Project window then choose "Export file". To "re-install" these, you right-click the "Modules" entry and choose "Import file".

    Using this latter approach is never a bad idea, as Word files can "go bad" and result in not being able to access the "macro storage" :-)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Mahesha999 Tuesday, April 12, 2011 3:12 PM
    Tuesday, April 12, 2011 2:46 PM
    Moderator
  • Thanks Cindy, great trick. If the editor had ever showed the extension .dotm (it shows only Normal instead of Normal.dotm), I could have found the way earlier. By the way its truly great & I am sure it will save many hours. Great, great!!!



    For those who search for the same, you will find the Normal.dotm at:

    C:\Users\<your user name>\AppData\Roaming\Microsoft\Templates

    (This is for Windows 7 & Office 2010)

    Just back it up before you format your system & copy at the same location when you reinstall the fresh system along with the fresh copy of the office. Your macros will be back again.




    Tuesday, April 12, 2011 3:03 PM
  • The Normal style (& also No Spacing style, that I tried) does many formatting without removing only the spaces above & below the paragraph.

    But thats OK, your comment gave me the idea. By the way it is very easy.

    The code for the macro to remove spaces above & below the selected paragraph is as follows:

     

    Sub RemoveSpaceAboveBelow()

        Selection.ParagraphFormat.SpaceAfter = 0

        Selection.ParagraphFormat.SpaceBefore = 0

    End Sub

     

    That was really easy. It wont change your selection font neither the size.

    Thank you all for your quick responses.


    • Marked as answer by Mahesha999 Tuesday, April 12, 2011 3:18 PM
    Tuesday, April 12, 2011 3:17 PM
  • Sub RemoveSpaceBeforePara()

        With Selection.ParagraphFormat
            .SpaceBefore = 0
            .SpaceBeforeAuto = False

        End With
    End Sub
    Sub RemoveSpaceAfterPara()

        With Selection.ParagraphFormat
            .SpaceAfter = 0
            .SpaceAfterAuto = False

        End With
    End Sub
    Monday, February 24, 2020 7:24 PM