Paragraph Alignment Microsoft Word Add-in code example

Paragraph Alignment Microsoft Word Add-in code example

Alignment

Paragraph object exposes Alignment property which takes various constants to perform text alignment of selected paragraphs derived from WdParagraphAlignment enum. In this article we will see all available alignments:

Constants

  1. wdAlignParagraphLeft: Left-aligned, constant value is 0
  2. wdAlignParagraphCenter: Center-aligned, constant value is 1
  3. wdAlignParagraphRight: Right-aligned, constant value is 2
  4. wdAlignParagraphJustify: Fully justified, constant value is 3
  5. wdAlignParagraphDistribute: Paragraph characters are distributed to fill the entire width of the paragraph, constant value is 4
  6. wdAlignParagraphJustifyMed: Justified with a medium character compression ratio, constant value is 5
  7. wdAlignParagraphJustifyHi: Justified with a high character compression ratio, constant value is 7
  8. wdAlignParagraphJustifyLow: Justified with a low character compression ratio, constant value is 8
  9. wdAlignParagraphThaiJustify: Justified according to Thai formatting layout, constant value is 9

Code example

Public Sub ParagraphFormat()
'Declare Paragraph object
    Dim oParagraph As Paragraph
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(1)
    'select paragraph text
    oParagraph.Range.Select
    'Center align
    oParagraph.Alignment = wdAlignParagraphCenter
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(2)
    'select paragraph text
    oParagraph.Range.Select
    'Distribute
    oParagraph.Alignment = wdAlignParagraphDistribute
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(3)
    'select paragraph text
    oParagraph.Range.Select
    'Justify
    oParagraph.Alignment = wdAlignParagraphJustify
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(4)
    'select paragraph text
    oParagraph.Range.Select
    'Justify High
    oParagraph.Alignment = wdAlignParagraphJustifyHi
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(5)
    'select paragraph text
    oParagraph.Range.Select
    'Justify Low
    oParagraph.Alignment = wdAlignParagraphJustifyLow
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(6)
    'select paragraph text
    oParagraph.Range.Select
    'Justify medium
    oParagraph.Alignment = wdAlignParagraphJustifyMed
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(7)
    'select paragraph text
    oParagraph.Range.Select
    'Justify Left
    oParagraph.Alignment = wdAlignParagraphLeft
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(8)
    'select paragraph text
    oParagraph.Range.Select
    'Justify right
    oParagraph.Alignment = wdAlignParagraphRight
    
    'Get paragraph
    Set oParagraph = ActiveDocument.Paragraphs(9)
    'select paragraph text
    oParagraph.Range.Select
    'Thai justify
    oParagraph.Alignment = wdAlignParagraphThaiJustify
End Sub

Output

C# code example

private void btnParaAlignment_Click(object sender, RibbonControlEventArgs e)
{
    //bind active document reference
    wordApp.Document oDocument = Globals.ThisAddIn.Application.ActiveDocument;

    //Paragraph object
    wordApp.Paragraph oParagraph = oDocument.Paragraphs[1];
    //Select paragraph
    oParagraph.Select();
    //Alignment Center
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphCenter;
    
    //Paragraph 2
    oParagraph = oDocument.Paragraphs[2];
    //Select paragraph
    oParagraph.Select();
    //Alignment Left
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphLeft;

    //Paragraph 3
    oParagraph = oDocument.Paragraphs[3];
    //Select paragraph
    oParagraph.Select();
    //Alignment Right
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphRight;
    
    //Paragraph 4
    oParagraph = oDocument.Paragraphs[4];
    //Select paragraph
    oParagraph.Select();
    //Alignment Justify
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustify;
    
    //Paragraph 5
    oParagraph = oDocument.Paragraphs[5];
    //Select paragraph
    oParagraph.Select();
    //Alignment Distribute
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphDistribute;
    
    //Paragraph 6
    oParagraph = oDocument.Paragraphs[6];
    //Select paragraph
    oParagraph.Select();
    //Alignment Med
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyMed;
    
    //Paragraph 7
    oParagraph = oDocument.Paragraphs[7];
    //Select paragraph
    oParagraph.Select();
    //Alignment Hi
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyHi;
    
    //Paragraph 8
    oParagraph = oDocument.Paragraphs[8];
    //Select paragraph
    oParagraph.Select();
    //Alignment Low
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyLow;
    
    //Paragraph 9
    oParagraph = oDocument.Paragraphs[9];
    //Select paragraph
    oParagraph.Select();
    //Alignment Thai
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphThaiJustify;
}

VB.Net code example

Private Sub btnParaAlignExample_Click(sender As Object, e As RibbonControlEventArgs) Handles btnParaAlignExample.Click

    //bind active document reference
    Dim oDocument AS wordApp.Document 
    oDocument = Globals.ThisAddIn.Application.ActiveDocument

    //Paragraph object
    wordApp.Paragraph oParagraph = oDocument.Paragraphs(1)
    //Select paragraph
    oParagraph.Select()
    //Alignment Center
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphCenter
    
    //Paragraph 2
    oParagraph = oDocument.Paragraphs(2)
    //Select paragraph
    oParagraph.Select()
    //Alignment Left
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphLeft

    //Paragraph 3
    oParagraph = oDocument.Paragraphs(3)
    //Select paragraph
    oParagraph.Select()
    //Alignment Right
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphRight
    
    //Paragraph 4
    oParagraph = oDocument.Paragraphs(4)
    //Select paragraph
    oParagraph.Select()
    //Alignment Justify
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustify
    
    //Paragraph 5
    oParagraph = oDocument.Paragraphs(5)
    //Select paragraph
    oParagraph.Select()
    //Alignment Distribute
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphDistribute
    
    //Paragraph 6
    oParagraph = oDocument.Paragraphs(6)
    //Select paragraph
    oParagraph.Select()
    //Alignment Med
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyMed
    
    //Paragraph 7
    oParagraph = oDocument.Paragraphs(7)
    //Select paragraph
    oParagraph.Select()
    //Alignment Hi
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyHi
    
    //Paragraph 8
    oParagraph = oDocument.Paragraphs(8)
    //Select paragraph
    oParagraph.Select()
    //Alignment Low
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphJustifyLow
    
    //Paragraph 9
    oParagraph = oDocument.Paragraphs(9)
    //Select paragraph
    oParagraph.Select()
    //Alignment Thai
    oParagraph.Alignment= wordApp.WdParagraphAlignment.wdAlignParagraphThaiJustify
End Sub

Next >> Paragraph Spacing Microsoft Word Add-in code example

Leave a Reply

Your email address will not be published. Required fields are marked *