How do you create a paragraph then later add it to the document

Mar 3, 2016 at 9:12 PM
I am wondering how I can create a Paragraph and then later add it to the document?

Thanks in advance!
    protected Paragraph MyProc(string txt)
    {
               Paragraph p;  
               p.Append("testing");

               return p;

    }
    protected Paragraph CallMyProc()
    {
          ....
           document.InsertParagraph(MyProc("testing"));

          ...
    }
Developer
Mar 5, 2016 at 11:09 AM
And what's not working for you ?
Mar 5, 2016 at 6:32 PM
Sorry I did not state this but the code above that created the Paragraph was more an example to fix what I should do.

I did not create a new instance of the paragraph
Paragraph p = new Paragraph();   (there was not a constructor that I saw or any others).
Paragraph p;

and then

p.Append("Testing"); is invalid since and instance of Paragraph was not created.
Mar 7, 2016 at 6:59 PM
@pkrug, yea, you can't create a new instance of Paragraph directly (because it has to built xml structure of docx).

But, you can do something like this:
    protected Paragraph MyProc(Paragraph p)
    {
        Paragraph p;  
        p.Append("testing");
        return p;
    }
    protected Paragraph CallMyProc()
    {
        var pNew = document.InsertParagraph(); //see overloads. 
        MyProc(pNew); //pNew is a ref to real paragraph, so you can change it and save your docx. 
    }