Microsoft Office Word document

Jedidia
Jedidia used Ask the Experts™
on
Need a little help here.

I am generating a word document and having a little trouble understanding the separation of paragraphs.

This code generates a word document (screen shot attached) and I am having issues formatting the paragraphs to the left.  the top paragraph and the bitmap I import should be centered.  the paragraph below (in the red box) should be shifted to the left.

                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                //Start Word and create a new document.

                // Get the Word application object.
                Word._Application oWord = new Word.Application();

                //Set animation status for word application
                oWord.ShowAnimation = false;

                //Make Word visible (optional)
                oWord.Visible = true;

                // Create the Word document.
                object missing = Type.Missing;
                Word._Document word_doc = oWord.Documents.Add(ref missing, ref missing, ref missing, ref missing);

                // Set the Range to the first paragraph. 
                Word.Range rng = word_doc.Paragraphs[1].Range;

                //Set Text and Alignment properties
                rng.Font.Size = 12;
                rng.Font.Name = "Arial";
                rng.Underline = WdUnderline.wdUnderlineSingle;
                rng.Font.Bold = 1;
                rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                rng.ParagraphFormat.SpaceAfter = 0;
                rng.ParagraphFormat.SpaceBefore = 0;
                rng.ParagraphFormat.LineSpacing = 12;
                rng.Text = txtStationName.Text + " TEST EQUIPMENT" + Environment.NewLine + "TEST TEST TEST";
                
                rng.InsertParagraphAfter();

               //Insert a paragraph as a spacer
                Word.Paragraph oPara2;
                object oRng1 = word_doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oPara2 = word_doc.Content.Paragraphs.Add(ref oRng1);
                oPara2.Range.Text = " ";
                oPara2.Format.SpaceAfter = 6;
                //oPara2.Format.Borders.Enable;// = true;// = 1;
                oPara2.Range.InsertParagraphAfter();
                
                Word.Paragraph concept = word_doc.Paragraphs.Add();

                // Add BitMap to WORD Document
                Bitmap croppedBitmap = new Bitmap(pictureBox1.Image);
                croppedBitmap = croppedBitmap.Clone(new System.Drawing.Rectangle(0, 0, picBoxSize_X, picBoxSize_Y), System.Drawing.Imaging.PixelFormat.DontCare);

                Clipboard.SetImage(croppedBitmap);      
                concept.Range.InsertParagraphAfter();
                concept.Range.Select();
                oWord.Selection.Paste();


                //Insert a paragraph at the end of the document.
                Word.Paragraph oPara3;
                object oRng = word_doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oPara3 = word_doc.Content.Paragraphs.Add(ref oRng);
                oPara3.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara3.Range.Text = "Notes: – TEST Equipment TEST TEST TEST ";
                oPara3.Format.SpaceAfter = 6;
                oPara3.Range.InsertParagraphAfter();

Open in new window


Pointers anyone...

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

Why don't you simply format after inserting the text

      oPara3.Range.Text = "Notes: – TEST Equipment TEST TEST TEST ";
      oPara3.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

Open in new window

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial