Click here to Skip to main content
12,399,799 members (48,392 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0 Word Microsoft , +
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 1-Apr-12 20:27pm
Updated 1-Apr-12 21:20pm
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??
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
vlad781 2-Apr-12 3:04am
   
I'm sorry but it did not. The first link is for a data table, and the second link is for something I'm not even using. If you take a closer look at my problem, the text align properties that I set are acting for the entire document and not each paragraph separately.

Thank you,
vlad
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi
I have the same problem in my project but your solution 2 is not working
so any other solutions??
  Permalink  
Comments
__TR__ 8-Feb-13 2:46am
   
Use "Have a Question or Comment?" button if you want to comment on a solution, so that the original poster will be notified. I would suggest you to ask this as a new question here[^]. And please delete this solution as its not an answer.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
  Permalink  
v4
Comments
vlad781 2-Apr-12 3:47am
   
Please explain the 1 star.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100