Friday, March 14, 2014

How increase, Decrease indent using word vba

Leave a Comment


Mostly to increase or decrease the indentation in word document, we use button increase or decrease indent in Home Tabs. In this article, I will show how to use the vba code to increase or decrease indentation. In this article, it include 6 types of the indentation, increase left indent, increase right indent, decrease left indent, decrease right indent, increase first line indent, and decrease first line indent. The following code show you the 6 types if indentation using vba by 0.5 cm.

Increase Left Indent
PublicSub IncreaseIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.LeftIndent + CentimetersToPoints(sDefaultIndent)
        opara.LeftIndent = sCalcul
    EndWith
   
    Set opara = Nothing
EndSub


Increase Right Indent
PublicSub IncreaseRIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.RightIndent + CentimetersToPoints(sDefaultIndent)
        opara.RightIndent = sCalcul
    EndWith
   
    Set opara = Nothing
EndSub

Increase First Line Indent
PublicSub IncreaseFIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.FirstLineIndent + CentimetersToPoints(sDefaultIndent)
        opara.FirstLineIndent = sCalcul
    EndWith
EndSub

Decrease Left Indent
PublicSub DecreaseIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.LeftIndent - CentimetersToPoints(sDefaultIndent)
        IfsCalcul < 0 Then sCalcul = 0
        opara.LeftIndent = sCalcul
    EndWith
EndSub

Decrease Right Indent
PublicSub DecreaseRIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.RightIndent - CentimetersToPoints(sDefaultIndent)
        IfsCalcul < 0 Then sCalcul = 0
        opara.RightIndent = sCalcul
    EndWith
EndSub

Decrease First Line Indent
PublicSub DecreaseFIndent()
    DimsDefaultIndent As Single
    DimsCalcul As Single
    Dimopara As Paragraph
   
    sDefaultIndent = 0.5
    WithSelection
        Set opara = .Paragraphs.First
        sCalcul = opara.FirstLineIndent - CentimetersToPoints(sDefaultIndent)
        IfsCalcul < 0 Then sCalcul = 0
        opara.FirstLineIndent = sCalcul
    EndWith
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 (IncreaseIndent, DecreaseIndent) and Click on Run

0 comments :

Post a Comment

DMCA.com Protection Status