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
Read More...

How to detect font font size and change font size using word vba

Leave a Comment


In this article, I will discuss on how to detect the font size and change the font size to the size as we want to change with two examples of code. In the first example, it is about changing the font size within the defined range of font size and in the second one; it is about changing all font size to one defined size.
To work with the font size with vba, we need to use Word.Range to detect the font size and change the font size. Here are the two example of codes


Example 1: Changing font size withthe defined font size

SubStandardizeFontSize (oWord As Word.Range)
    IfoWord.Font.Size < 16 And oWord.Font.Size > 12 Then
        oWord.Font.Size = 16
    ElseIfoWord.Font.Size < 12 And oWord.Font.Size > 11 Then
        oWord.Font.Size = 12
    ElseIfoWord.Font.Size <= 11 And oWord.Font.Size > 9 Then
        oWord.Font.Size = 11
    ElseIfoWord.Font.Size <= 9 Then
        oWord.Font.Size = 10
    ElseIfoWord.Font.Size = 9999999 Then'999999 - represents word with characters of different size
        sFontSize = oWord.Characters(1).Font.Size
        Forj = 1 To oWord.Characters.Count
            IfoWord.Characters(j).Font.Size = sFontSize Then: oWord.Characters(j).Font.Size = 12
            Else: oWord.Characters(j).Font.Size = 10
            End If
        Next
    End If
EndSub

Example 2: Change font size to a 12 pt
PublicSub ChangeFontSize()
    DimoParagraph As Paragraph
    DimoRange As Range
    Dimi, j As Integer
    DimsFontSize As Single
   
    ForEach oParagraph In Selection.Paragraphs
        Set oRange = oParagraph.Range
        Fori = 1 To oRange.Words.Count
            IfoRange.Words(i).Font.Size = 9999999 Then'999999 - represents word with characters of different size
                sFontSize = oRange.Words(i).Characters(1).Font.Size
                Forj = 1 To oRange.Words(i).Characters.Count
                    IfoRange.Words(i).Characters(j).Font.Size = sFontSize Then
                        oRange.Words(i).Characters(j).Font.Size = 12
                    Else
                        oRange.Words(i).Characters(j).Font.Size = 12
                    End If
                Next
            End If
        Next
    Next
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 StandardizeFontSize and Click on Run
Read More...