473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Paragraph Alignment when Creating a Word Document- VB.NET

1 New Member
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.

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).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}
Oct 23 '08 #1
2 20131
kornels
1 New Member
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.
Jul 31 '10 #2
mertus
1 New Member
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
1252
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, ref noValue, ref noValue, ref noValue); objWordDoc.Activate(); I then get the data from the database and load it into
2
1595
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: System.UnauthorizedAccessException: Access is denied. The ASP.NET process is not authorized to access the requested resource. For
0
989
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 objects in the view is called. The question here is: if the destructors of the view's attribute objects in wasn't called, how can they be...
4
2689
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 the final output as a Word document. I know there are different options to do this. But I can't decide which one I should use. I don't want to use...
1
3954
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: http://www.dotnetspider.com/Technology/KBPages/587.aspx http://www.codeproject.com/aspnet/wordapplication.asp But if I follow the instructions provided in the second...
2
6082
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 = "N:\Checklist.dot" Public Sub PrintDocument()
1
10966
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 format will be fixed.So please can anybody help me with the Word VB Excel connectivity as soon as possible.
5
1158
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 importing am not getting using System.Microsoft.Office.Interop.Word why this is happening how to achieve this please help itz very urgent to me ...
0
1108
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 gets activated (A WINWORD.EXE process is opened) and i can edit the document and in the button click event i am saving the document like this : ...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.