Paragraph Alignment when Creating a Word Document- VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uamusa
    New Member
    • Oct 2008
    • 1

    #1

    Paragraph Alignment when Creating a Word Document- VB.NET

    I am Dynamically generating a proposal(report ) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to switch back to "Left" aligned for remaining paragraphs, the text within the Word document remained Centered.

    I'm using: VS2008 w/ .NET Framework 3.5, Microsoft.Offic e.Interop.Word Version 12.0.0.0, and I'm creating the document as a 97-2003 document though in my test environment the document is opening in Word 2007.

    Code:
    'Create Word Application
                    g_oWord = CreateObject("Word.Application")
    
                    'Create new word document
                    g_oDoc = g_oWord.Documents.Add() '"C:\GraphicFile.jpg"
    
                    'Dim oHeader As Word.HeaderFooter
                    'Dim oSection(3) As Word.Section
    
                    'Make Word Document Invisible
                    g_oWord.Visible = False
    
                    'Insert Each Field after the previous
                    Dim oPara(8) As Word.Paragraph
                    Dim x As Integer = 1
    
                    'oRange(0) = oWord.ActiveDocument
                    oPara(0) = g_oDoc.Content.Paragraphs.Add
                    oPara(0).Range.Font.Bold = True
                    oPara(0).Range.Font.Size = 22
                    oPara(0).Range.Text = vbNewLine
                    oPara(0).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    
                    oPara(1) = g_oDoc.Content.Paragraphs.Add
                    oPara(1).Range.Font.Bold = True
                    oPara(1).Range.Font.Size = 22
                    oPara(1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                    oPara(1).Range.Text = "List Each Product Here" & vbNewLine & "Proposal" & vbNewLine & vbNewLine
    
                    oPara(2) = g_oDoc.Content.Paragraphs.Add
                    oPara(2).Range.Font.Bold = True
                    oPara(2).Range.Font.Size = 20
                    oPara(2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                    oPara(2).Range.Text = "Presented to" & vbNewLine & vbNewLine
    
                    oPara(3) = g_oDoc.Content.Paragraphs.Add
                    oPara(3).Range.Font.Bold = True
                    oPara(3).Range.Font.Size = 18
                    oPara(3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                    oPara(3).Range.Text = "Customer Name" & vbNewLine & vbNewLine
    
                    oPara(4) = g_oDoc.Content.Paragraphs.Add
                    oPara(4).Range.Font.Bold = True
                    oPara(4).Range.Font.Size = 18
                    oPara(4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                    oPara(4).Range.Text = "by" & vbNewLine & "Agency Name" & vbNewLine & "Proposal Date" & vbNewLine & vbNewLine & vbNewLine & vbNewLine
    
                    oPara(5) = g_oDoc.Content.Paragraphs.Add
                    oPara(5).Range.Font.Bold = False
                    oPara(5).Range.Font.Size = 10
                    oPara(5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                    oPara(5).Range.InlineShapes.AddPicture(logo)
    
                    oPara(6) = g_oDoc.Content.Paragraphs.Add
                    oPara(6).Range.Font.Italic = True
                    oPara(6).Range.Font.Size = 10
                    oPara(6).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
                    oPara(6).Range.Text = "" & vbNewLine & "Company Name Here"
    
                    oPara(7) = g_oDoc.Content.Paragraphs.Add
                    oPara(7).Range.Font.Bold = False
                    oPara(7).Range.Font.Size = 12
                    oPara(7).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
                    oPara(7).Range.Text = ""
    When stepping thru the code in Debug, both the "oPara(x).Align ment" & "oPara(x).Forma t.Microsoft.Off ice.Interop.Wor d.ParagraphForm atClass.Alignme nt" are being assigned the correct values: wdAlignParagrap hLeft{0}
  • kornels
    New Member
    • Jul 2010
    • 1

    #2
    This is an very old thread, but maybe someone else seeks the answer as I did can find it here.

    the answer is that you need to set the alignment after the text is inserted, meaning not this:

    oPara(1) = g_oDoc.Content. Paragraphs.Add
    oPara(1).Range. Font.Bold = True
    oPara(1).Range. Font.Size = 22
    oPara(1).Range. ParagraphFormat .Alignment = Word.WdParagrap hAlignment.wdAl ignParagraphCen ter
    oPara(1).Range. Text = "List Each Product Here" & vbNewLine & "Proposal" & vbNewLine & vbNewLine

    but this

    oPara(1) = g_oDoc.Content. Paragraphs.Add
    oPara(1).Range. Font.Bold = True
    oPara(1).Range. Font.Size = 22
    oPara(1).Range. Text = "List Each Product Here" & vbNewLine & "Proposal" & vbNewLine & vbNewLine
    oPara(1).Range. ParagraphFormat .Alignment = Word.WdParagrap hAlignment.wdAl ignParagraphCen ter


    This solved the issue for me after also struggling to get the alignment to change at all.

    Comment

    • mertus
      New Member
      • Aug 2012
      • 1

      #3
      we should spread this information:) thanks a lot.

      Comment

      Working...