Word automation - paragraph alignment issues

  • Thread starter Thread starter Seekaye
  • Start date Start date
S

Seekaye

Guest
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.

Continue reading...
 
Back
Top