none
Word automation - paragraph alignment issues RRS feed

  • Question

  • Hi,

    I'm producing a word document. This code works as I expect

     Dim OWord As Word.Application
    
            'Get an application instance
            OWord = GetObject("", "Word.Application")
    
            'Create new Document
            Dim oDoc As New Word.Document
            oDoc = OWord.Documents.Add
    
            'For now, make visible
            OWord.Visible = True
    
    
            Dim Para1, Para2, Para3 As Word.Paragraph
    
            Para1 = oDoc.Paragraphs.Add
            Para1.Range.Font.Size = 14
            Para1.Range.Bold = True
            Para1.Range.Text = "Want Size 14 Bold, centre aligned"
            Para1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            Para1.Range.InsertParagraphAfter()
    
            Para2 = oDoc.Paragraphs.Add
            Para2.Range.Font.Size = 11
            Para2.Range.Bold = False
            Para2.Range.Underline = True
            Para2.Range.Text = "Want Size 11 underlined, not bold, left aligned"
            Para2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Para2.Range.InsertParagraphAfter()
    
            Para3 = oDoc.Paragraphs.Add
            Para3.Range.Font.Size = 8
            Para3.Range.Bold = False
            Para3.Range.Underline = False
            Para3.Range.Text = "Want Size 8 not underlined, not bold, right aligned"
            Para3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            Para3.Range.InsertParagraphAfter()

    However, if I try and add a line break at the end of each line:

     Dim OWord As Word.Application
    
            'Get an application instance
            OWord = GetObject("", "Word.Application")
    
            'Create new Document
            Dim oDoc As New Word.Document
            oDoc = OWord.Documents.Add
    
            'For now, make visible
            OWord.Visible = True
    
    
            Dim Para1, Para2, Para3 As Word.Paragraph
    
            Para1 = oDoc.Paragraphs.Add
            Para1.Range.Font.Size = 14
            Para1.Range.Bold = True
            Para1.Range.Text = "Want Size 14 Bold, centre aligned" & vbcrlf
            Para1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            Para1.Range.InsertParagraphAfter()
    
            Para2 = oDoc.Paragraphs.Add
            Para2.Range.Font.Size = 11
            Para2.Range.Bold = False
            Para2.Range.Underline = True
            Para2.Range.Text = "Want Size 11 underlined, not bold, left aligned" & vbcrlf
            Para2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            Para2.Range.InsertParagraphAfter()
    
            Para3 = oDoc.Paragraphs.Add
            Para3.Range.Font.Size = 8
            Para3.Range.Bold = False
            Para3.Range.Underline = False
            Para3.Range.Text = "Want Size 8 not underlined, not bold, right aligned" & vbcrlf
            Para3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            Para3.Range.InsertParagraphAfter()

    Then the line alignments mess up, the para1 is left aligned (presumably default), the para2 is centre aligned (like I want para1 to be) and para 3 is left aligned (like i want para2 to be)

    Any help appreciated.

    Sunday, February 17, 2019 1:08 PM

All replies

  • If you like I can move your question to the Word for developers forum where you would have a better chance of assistance. Personally I've only done what you are after in C# and OpenXML so I have nothing to offer especially since it's been sometime doing this.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, February 17, 2019 3:19 PM
  • Yes that'd be great, thanks for your help
    Sunday, February 17, 2019 5:38 PM