Friday, March 14, 2014

How to Connect Paragraph using word vba

Leave a Comment


As I mentioned in the previous article, VBA allow us to automate your tasks, and your will be done quickly with vba because vba programming can perform many procedure at the same time. In this article, I will show how to connect two or more paragraphs together by just select the paragraph and then run the macro. Normally, when we want to connect to paragraph together, we need to delete the paragraph mark.
Suppose that we want to connect 20 paragraphs in word document, we need to delete the paragraph mark 20 times. However with vba programming, we can delete the 20 paragraphs marks at once.

How to delete the Paragraph Marks
Word VBA provides us the Selection object to word to selection text, paragraph, etc.

In order to delete paragraph mark using selection, first we need to select the paragraph using selection object
Select 5 paragraph from the position of cursor down


Selection.MoveDown Unit:=wdParagraph, Count:=5, Extend:=wdExtend

detect the paragraph and delete


Ifopara.Range.Words.Count > 1 Then
            IfisDeleted Then ExitFor
        Else
            opara.Range.Delete
            isDeleted = True
        End If

then use selection.Find to find the paragraph mark and replace with space

    WithSelection.Find
        .ClearFormatting
        .Text = "^p"
        .Replacement.ClearFormatting
        .Replacement.Text = " "
        .Wrap = wdFindStop
        .Forward = True
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceOne
    EndWith
   


Full Code of this article

PublicSub ConnectParagraph()
    Dimopara As Paragraph
    DimisDeleted, isFirst As Boolean
    DimsLIndent, sRIndent, sSBefore, sSAfter, sFLIndent As Single
   

    isDeleted = False
    isFirst = True
    Selection.MoveDown Unit:=wdParagraph, Count:=5, Extend:=wdExtend
    ForEach opara In Selection.Paragraphs
        Ifopara.Range.Words.Count > 1 Then
            IfisDeleted Then ExitFor
        Else
            opara.Range.Delete
            isDeleted = True
        End If
       
        ' Save format
        IfisFirst Then
            sLIndent = opara.LeftIndent
            sRIndent = opara.RightIndent
            sSBefore = opara.SpaceBefore
            sSAfter = opara.SpaceAfter
            sFLIndent = opara.FirstLineIndent
        End If
       
        isFirst = False
    Next opara
   
    WithSelection.Find
        .ClearFormatting
        .Text = "^p"
        .Replacement.ClearFormatting
        .Replacement.Text = " "
        .Wrap = wdFindStop
        .Forward = True
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceOne
    EndWith
   
    WithSelection.Paragraphs(1)
        .FirstLineIndent = sFLIndent
        .LeftIndent = sLIndent
        .RightIndent = sRIndent
        .SpaceBefore = sSBefore
        .SpaceAfter = sSAfter
    EndWith
   
    ' Move back to the beginning of the paragraph
    Selection.Collapse wdCollapseStart
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 ConnectParagraph and Click on Run

0 comments :

Post a Comment

DMCA.com Protection Status