473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Paragraph Alignment when Creating a Word Document- VB.NET

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
2 20086
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
mertus
1
we should spread this information:) thanks a lot.
Aug 9 '12 #3

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

Similar topics

0
by: edwaldo | last post by:
Hi I'm using Word.ApplicationClass in C# to produce a Word document: Word.ApplicationClass objWord = new Word.ApplicationClass(); Word.Document objWordDoc = objWord.Documents.Add(ref noValue,...
2
by: Arvind P Rangan | last post by:
Hi all, trying to create word document using following syntax in c# private Word.ApplicationClass oWordApp = new Word.ApplicationClass(); Application throws error as : Exception Details:...
0
by: forest428 | last post by:
I found an interesting issue in SDI. In SDI, when i create a new document, none of the constructor or the destructor of the view is called, and none of the constructor or destructor of the attribute...
4
by: Nikhil Patel | last post by:
Hi all, I need to generate a word document and save it on the server from an ASP.Net application. Basically I want to load a word template and insert some field values from a dataset and save...
1
by: Novice | last post by:
Hi all, I'm using C# in my ASP.NET application and cannot seem to access the MS word libraries to create a word document for some reason. I have used both of these tutorials:...
2
by: Chris Mahoney | last post by:
Hi I'm trying to print a Word document from my VB 2005 app, but I'm having trouble. Imports Microsoft.Office.Interop Private oWord As Word.Application Private WordTemplate As Object =...
1
by: sharayu | last post by:
Hi I am doing a small project in vb.I want to create a word document consisting values in table format.The values are to be taken from the excel files.This is to be done using VB.The word file...
5
by: Vajrala Narendra | last post by:
Hi all, am working on asp.net 2.0, c# i want to create a word document through coding for that i add reference from COM Microsoft word 11.0 object library, type lib version 8.3 but while...
0
JAMBAI
by: JAMBAI | last post by:
I am using a OLE unbound Control to Embed the word document in the form load, event like this http://support.microsoft.com/kb/209990 and When i double clicks (In Place activation), the documents...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.