Friday, March 14, 2014

How to remove manual line break using word vba

Leave a Comment


This part of code is for removing the all Manual Line Break in word document at once.

What is Manual line break?
A manual line break ends the current line and continues the text on the next line. For example, suppose your paragraph style includes extra space before each paragraph.

To get Manual Line Break
1. Click where you want to break a line of text.
2. Press SHIFT+ENTER.

How to remove
To remove Manual we need to search the for manual line break and replace it with space or nothing
Find Manual Line Break
Selection.Find.Text = "^l"

Replace with space
Selection.Find.Replacement.Text =  ""

Full code of this article
PublicSub RemoveManualLineBreak()
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    WithSelection.Find
        .Text = "^l^t"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    EndWith
    Selection.Find.Execute Replace:=wdReplaceAll
   
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    WithSelection.Find
        .Text = "^l"
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    EndWith
    Selection.Find.Execute Replace:=wdReplaceAll
   
    Selection.HomeKey Unit:=wdStory
EndSub

How to test this Code
1. Open Ms Word
2. Open VB Editor or Alt+F11
3. Create new module
4. Copy and paste the Code
5. Go back to you word and go to Macro Dialog then select the Macro Name (RemoveManualLineBreak) and Click on Run

0 comments :

Post a Comment

DMCA.com Protection Status