By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,725 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,725 IT Pros & Developers. It's quick & easy.

Paragraph Alignment when Creating a Word Document- VB.NET

P: 1
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.Office.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.

Expand|Select|Wrap|Line Numbers
  1. 'Create Word Application
  2.                 g_oWord = CreateObject("Word.Application")
  3.  
  4.                 'Create new word document
  5.                 g_oDoc = g_oWord.Documents.Add() '"C:\GraphicFile.jpg"
  6.  
  7.                 'Dim oHeader As Word.HeaderFooter
  8.                 'Dim oSection(3) As Word.Section
  9.  
  10.                 'Make Word Document Invisible
  11.                 g_oWord.Visible = False
  12.  
  13.                 'Insert Each Field after the previous
  14.                 Dim oPara(8) As Word.Paragraph
  15.                 Dim x As Integer = 1
  16.  
  17.                 'oRange(0) = oWord.ActiveDocument
  18.                 oPara(0) = g_oDoc.Content.Paragraphs.Add
  19.                 oPara(0).Range.Font.Bold = True
  20.                 oPara(0).Range.Font.Size = 22
  21.                 oPara(0).Range.Text = vbNewLine
  22.                 oPara(0).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  23.  
  24.                 oPara(1) = g_oDoc.Content.Paragraphs.Add
  25.                 oPara(1).Range.Font.Bold = True
  26.                 oPara(1).Range.Font.Size = 22
  27.                 oPara(1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  28.                 oPara(1).Range.Text = "List Each Product Here" & vbNewLine & "Proposal" & vbNewLine & vbNewLine
  29.  
  30.                 oPara(2) = g_oDoc.Content.Paragraphs.Add
  31.                 oPara(2).Range.Font.Bold = True
  32.                 oPara(2).Range.Font.Size = 20
  33.                 oPara(2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  34.                 oPara(2).Range.Text = "Presented to" & vbNewLine & vbNewLine
  35.  
  36.                 oPara(3) = g_oDoc.Content.Paragraphs.Add
  37.                 oPara(3).Range.Font.Bold = True
  38.                 oPara(3).Range.Font.Size = 18
  39.                 oPara(3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  40.                 oPara(3).Range.Text = "Customer Name" & vbNewLine & vbNewLine
  41.  
  42.                 oPara(4) = g_oDoc.Content.Paragraphs.Add
  43.                 oPara(4).Range.Font.Bold = True
  44.                 oPara(4).Range.Font.Size = 18
  45.                 oPara(4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  46.                 oPara(4).Range.Text = "by" & vbNewLine & "Agency Name" & vbNewLine & "Proposal Date" & vbNewLine & vbNewLine & vbNewLine & vbNewLine
  47.  
  48.                 oPara(5) = g_oDoc.Content.Paragraphs.Add
  49.                 oPara(5).Range.Font.Bold = False
  50.                 oPara(5).Range.Font.Size = 10
  51.                 oPara(5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
  52.                 oPara(5).Range.InlineShapes.AddPicture(logo)
  53.  
  54.                 oPara(6) = g_oDoc.Content.Paragraphs.Add
  55.                 oPara(6).Range.Font.Italic = True
  56.                 oPara(6).Range.Font.Size = 10
  57.                 oPara(6).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
  58.                 oPara(6).Range.Text = "" & vbNewLine & "Company Name Here"
  59.  
  60.                 oPara(7) = g_oDoc.Content.Paragraphs.Add
  61.                 oPara(7).Range.Font.Bold = False
  62.                 oPara(7).Range.Font.Size = 12
  63.                 oPara(7).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
  64.                 oPara(7).Range.Text = ""
When stepping thru the code in Debug, both the "oPara(x).Alignment" & "oPara(x).Format.Microsoft.Office.Interop.Word.Par agraphFormatClass.Alignment" are being assigned the correct values: wdAlignParagraphLeft{0}
Oct 23 '08 #1
Share this Question
Share on Google+
2 Replies

P: 1
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.WdParagraphAlignment.wdAlignParagraphCenter
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.WdParagraphAlignment.wdAlignParagraphCenter


This solved the issue for me after also struggling to get the alignment to change at all.
Jul 31 '10 #2

P: 1
we should spread this information:) thanks a lot.
Aug 9 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.