Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
C#
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
            
            //Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);


This is where I run into the problem:

//Insert a paragraph at the beginning of the document.
            Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            Word.Range rng = oPara1.Range;
            rng.Font.Size = 14;
            rng.Font.Name = "Arial";
            rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            oPara1.Range.Font.Bold = 1;
            oPara1.Range.Text = "SOCIAL ASSESSMENT";
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();
            
            
            //Insert another paragraph.
            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            Word.Paragraph oPara2;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
            oPara2.Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
            oPara2.Range.Font.Bold = 1;
            oPara2.Range.Text = "Request for Services and Identifying Information";
            oPara2.Format.SpaceAfter = 24;
            oPara2.Range.InsertParagraphAfter();

            //Insert another paragraph.
            Word.Paragraph oPara3;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara3.Range.Font.Bold = 0;
            oPara3.Range.Text = "Applicants Name: " + textBox1.Text + " " + textBox7.Text + "\r\n" + "Referred by: "
                + textBox2.Text + "\r\n" + "Request for Evaluation by: " + textBox3.Text + "\r\n" +
                "Reason for Referral: " + textBox4.Text + "\r\n" + "Service Request: " + textBox5.Text
                + "\r\n" + "Clients Primary Language: " + textBox6.Text + "\r\n" +
                "Language of Informant/Advocate: " + textBox8.Text;
            oPara3.Format.SpaceAfter = 24;
            oPara3.Range.InsertParagraphAfter();


The entire document ends up being left-aligned. How can I fix this?

Thank you,
Vlad
Posted
Updated 3-May-18 21:45pm
v4
Comments
manikantaer 8-Feb-13 3:45am    
Hi
I have the same problem in my project but your solution 2 is not working
so any other solutions??

I was able to solve this myself. I had to specify exactly which range to align:

Word.Range rng = oPara1.Range;
rng.Font.Size = 14;
rng.Font.Name = "Arial";
rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;


And now left align the next paragraph:

Word.Range rng2 = oPara2.Range;
rng2.Font.Size = 12;
rng2.Font.Name = "Arial";
rng2.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;



This centers the first line of text, while left aligning the rest :)
 
Share this answer
 
v4
Comments
vlad781 2-Apr-12 3:47am    
Please explain the 1 star.
I know this is old and closed question, but the solution mentioned here didn't help me.
So i'll post the solution here for ppl like me in hope it will help them.

The thing you need to do is set the Alignment property AFTER the Text property (see the code below).

// p1 is going to be right aligned
var p1 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p1.Range.Font.Name = "Calibri";
p1.Range.Font.Size = 18;
p1.Range.Text = "right";
p1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
p1.Range.InsertParagraphAfter();
// p2 is going to be center aligned
var p2 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p2.Range.Font.Name = "Calibri";
p2.Range.Font.Size = 16;
p2.Range.Text = "center";
p2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
p2.Range.InsertParagraphAfter();
// p3 is going to be left aligned
var p3 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p3.Range.Font.Name = "Calibri";
p3.Range.Font.Size = 14;
p3.Range.Text = "left";
p3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
p3.Range.InsertParagraphAfter();


This is the way it worked for me.
 
Share this answer
 
Comments
Maciej Los 4-May-18 3:49am    
Why to answer already answered question? What is the added value of your answer?
Rudolf Németh 4-May-18 4:08am    
As i mentioned the solution didn't help me and what vlad781 wrote in his answer afaik wasn't the case to this problem. The case was as i mentioned the order in which you set the values of properties (you need to set the value of alignment property after you set the value of text property not the other way around). The solution is for ppl like me that was looking for the answer and didn't found it here (see manikantaers post here marked as solution3)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900