0

I am using OpemXML in C# in order to build my DOCX file. My code looks like this:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(wordFileNamePath, true))
{
    for (int i = 0; i < length; i++)
    {
        using (StreamWriter sw = new StreamWriter(i == 0 ? wordDoc.MainDocumentPart.GetStream(FileMode.Create) : wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write)))
        {
            sw.Write(tempDocText.ToString());
        }
        if (i < length - 1)
        {
            tempDocText = CreateNewStringBuilder();
            InsertPageBreak(wordDoc);
        }
    }
    wordDoc.MainDocumentPart.Document.Save();
}

On the second loop, when it comes to wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write) I'm getting an ArgumentException saying "FileMode value is not supported."

yazanpro
  • 4,512
  • 6
  • 44
  • 66

1 Answers1

0

I think there is a problem in your code, you are using tempDocText.ToString() before initializing it in the for loop as shown below

using (StreamWriter sw = new StreamWriter(i == 0 ? wordDoc.MainDocumentPart.GetStream(FileMode.Create) : wordDoc.MainDocumentPart.GetStream(FileMode.Append, FileAccess.Write)))
{
    sw.Write(tempDocText.ToString()); //<-Used before Initialization
}

and Initializing it as in the later code block

if (i < length - 1)
{
    tempDocText = CreateNewStringBuilder(); //<-Initializing it here.
    InsertPageBreak(wordDoc);
}

Unless you provide more information about tempDocText, its dificult to help.

Anyways, if you just want to add text to a docx file then the following code may help. I found it here.

public static void OpenAndAddTextToWordDocument(string filepath, string txt)
{   
    // Open a WordprocessingDocument for editing using the filepath.
    WordprocessingDocument wordprocessingDocument = 
        WordprocessingDocument.Open(filepath, true);

    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

    // Add new text.
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text(txt));

    // Close the handle explicitly.
    wordprocessingDocument.Close();
}
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • The code is bigger than what I posted but the full code won't make any difference. As you said, initializing tempDocText should be made before the loop. And 'length' should be initialized as well. My goal is not to append a simple text, I need to append an xml (after some modifications). this xml is taken from another DOCX file – yazanpro Apr 16 '13 at 19:03
  • Then maybe this [answer](http://stackoverflow.com/a/8818812/1012641) will help you – Patrick D'Souza Apr 16 '13 at 19:07
  • That guy in the answer you provided is not appending, he is doing as I'm doing when i = 0. and then he closes the wordProcessingDocument and save his stream do a new docx file – yazanpro Apr 16 '13 at 19:23