Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C# C#4.0 Word , +
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
Edited 1-Apr-12 21:20pm
v4
Comments
manikantaer at 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 at 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 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 at 2-Apr-12 3:47am
   
Please explain the 1 star.
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__ at 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.

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

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 RyanDev 260
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 185
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2014
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