none
changing paragraph alignment RRS feed

  • Question

  • I am inserting paragraphs into a Word document from a VSTO Word addin.  Click a button on the ribbon, the addin inserts the paragraphs into the document. Problem is, I cannot change the paragraph alignment from one paragraph to the next. All paragraphs are Center aligned. I want the first two to be centered. Then the 3rd and 4th paragraphs to be aligned to the left.  How to do this?

    here is my code.  thanks,

          {
            InteropWord.Paragraph para1 = doc.Content.Paragraphs.Add(ref missing);
            object styleHeading1 = "Heading 1";
            para1.Range.set_Style(ref styleHeading1);
            para1.Range.Text = "Invoice";
            para1.Range.ParagraphFormat.Alignment = InteropWord.WdParagraphAlignment.wdAlignParagraphCenter;
            para1.Range.InsertParagraphAfter();
          }
    
          {
            InteropWord.Paragraph para1 = doc.Content.Paragraphs.Add(ref missing);
            para1.Range.Text = DateTime.Now.ToString("MMMM d, yyyy");
            para1.Range.Font.Size = 14;
            para1.Range.ParagraphFormat.Alignment = InteropWord.WdParagraphAlignment.wdAlignParagraphCenter;
            para1.Range.InsertParagraphAfter();
          }
    
          {
            InteropWord.Paragraph para1 = doc.Content.Paragraphs.Add(ref missing);
            para1.Range.ParagraphFormat.Alignment = InteropWord.WdParagraphAlignment.wdAlignParagraphLeft;
    
            var pt = Application.InchesToPoints(4.25f);
            para1.Range.Paragraphs.TabStops.Add(pt, InteropWord.WdTabAlignment.wdAlignTabLeft);
            para1.Range.Font.Size = 12;
            para1.Range.Text = "Company name, LLC" + "\t" + "Invoice #: C8504" + "\v" +
              "P.O. BOX 99" + "\v" +
              "Town, NJ  zip";
            para1.Range.InsertParagraphAfter();
          }
    
          {
            InteropWord.Paragraph para1 = doc.Content.Paragraphs.Add(ref missing);
            para1.Range.ParagraphFormat.Alignment = InteropWord.WdParagraphAlignment.wdAlignParagraphLeft;
            para1.Range.Font.Size = 12;
            para1.Range.Text = "Bill to:" + "\v" + "Company" + "\v" + "IT Department" + "\v" +
              "street address" + "\v" +
              "city, state  zip";
            para1.Range.InsertParagraphAfter();
          }
    

    Saturday, January 14, 2017 2:09 PM

Answers

  • Hi Steve,

    Change the Style for 3rd and 4th paragraphs to a left aligned paragraph... something other than Heading 1. What your code is doing is "direct formatting the same style with different font size and alignment characteristics. In theory that can work but it also can backfire on you and reset to the default settings if the style is reapplied later in your code.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by Steve Richter Sunday, January 15, 2017 6:59 PM
    Sunday, January 15, 2017 1:14 PM