none
Edit Open Word Document with OpenXml RRS feed

  • Question

  • First time poster, short time reader.

    I have been spending a significant amount of time attempting to modify an open Word document using C# and openXML. I was wondering if I can get some assistance on getting some code that would allow me to add a paragraph of text into a document. I am able to do this using Microsoft.Office.Interop.Word but no success using using DocumentFormat.OpenXml.Wordprocessing; Any help is greatly appreciated. If someone if really good with OpenXml and Word, I can pay a few hundred dollars to get help on a program I am working. THanks!!!!

     private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Globals.ThisAddIn.AddText();
            }

    Word Interop

    public void AddText()

    {
     Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
     rng.Text = "I just added this crap using my brain, after about a month will it work";
     }

    OpenXml

    public void AddText()

    using (WordprocessingDocument doc = WordprocessingDocument.Open()) <-----Don't know how to get MainDocument referenced for already open document.
    {   // Add a main document part.
    MainDocumentPart mainPart = doc.AddMainDocumentPart();

    // Create the document structure and add some text.
    Text text = new Text("This is a new paragraph I'm inserting");
    mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
    Body body = mainPart.Document.AppendChild(new Body());
    DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
     Run run = para.AppendChild(new Run(text));

    }

    Wednesday, August 16, 2017 10:59 PM

All replies

  • Hi Ryzen-1981,

    As far as I know, we could not use DocumentFormat.OpenXml.Wordprocessing to open an already opened word document. Since you could do this using  Microsoft.Office.Interop.Word, I'm wondering why you still need to do this using Open XML? Is there any requirement that OpenXML could accomplish but Microsoft.Office.Interop.Word could not?

    Best Regards,

    Terry

    Thursday, August 17, 2017 5:45 AM
  • Thank you Terry for the Response. I thought that working directly with OpenXML to edit Word Documents would be the best option as you can call out specific paragraphs and text edits versus the builtin word items. If this is not the case, do you know where I can find some good walkthroughs on how to modify paragraphs and headings using the interops

    Greatly appreciate your help!!!

    J

    Thursday, August 17, 2017 10:12 AM
  • Hi Ryzen-1981,

    How do you want to modify the paragraphs? You could get use Paragraphs collection to get a paragraph object.

    Then you could modify the paragraph, such as

                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                Word.Paragraph paragraph = doc.Paragraphs[1];
                paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                paragraph.Range.Text = "Change first paragraph text as this";

    Besides, you could also use Paragraphs.Add to add a new paragraph to a specific range.

    Best Regards,

    Terry

    Tuesday, August 22, 2017 12:39 AM