Saving Document stream after replacing text destroys file

Hello,

I am using the document.Range.Replace method to replace Text in a word document. Normally i prefer working with streams, but in this case working with stream destroy the file after replacing text in it.

I made a sample method:

public override void ApplyTextTest()
{
    // First I use a regular file
    string fileName = "C:\Temp\Test";
    Document doc = new Document(fileName + ".docx");
    doc.Range.Replace("##Company.CompanyPart.CompanyDesc##", "replace it", new FindReplaceOptions());
    doc.Range.Replace("##=US.TestFunct.CompanyCode##", "replace it too", new FindReplaceOptions());
    doc.Range.Replace("##=US.TestFunct.Auchschon##", "replace it also", new FindReplaceOptions());
    // i am saving the result into another file (could save it to the original file, which would also work)
    doc.Save(fileName + "_replaced.docx");
    // this works fine

    // now i take the origin file and open it in a stream
    FileStream stream = new FileStream(fileName + ".docx", FileMode.Open, FileAccess.ReadWrite);
    try
    {
        doc = new Document(stream);
        doc.Range.Replace("##Company.CompanyPart.CompanyDesc##", "replace it", new FindReplaceOptions());
        doc.Range.Replace("##=US.TestFunct.CompanyCode##", "replace it too", new FindReplaceOptions());
        doc.Range.Replace("##=US.TestFunct.Auchschon##", "replace it also", new FindReplaceOptions());
        // i am saving the changes to the original stream
        doc.Save(stream, SaveFormat.Docx);
    }
    finally
    {
        stream.Close();
        stream.Dispose();
    }
    // now the file is rendered unusable…
}

Am I doing something wrong?

Thanks in advance

Stefan

Hi Stefan,

Thanks for your inquiry. Please use following code example to fix this issue. Hope this helps you.

FileStream stream = new FileStream(MyDir + "input.docx", FileMode.Open, FileAccess.Read);
try
{
    Document doc = new Document(stream);
    stream.Close();
    stream.Dispose();
    doc.Range.Replace("##Company.CompanyPart.CompanyDesc##", "replace it", new FindReplaceOptions());
    doc.Range.Replace("##=US.TestFunct.CompanyCode##", "replace it too", new FindReplaceOptions());
    doc.Range.Replace("##=US.TestFunct.Auchschon##", "replace it also", new FindReplaceOptions());
    stream = new FileStream(MyDir + " input.docx ", FileMode.Create, FileAccess.Write);
    // i am saving the changes to the original stream
    doc.Save(stream, SaveFormat.Docx);
}
finally
{
    stream.Close();
    stream.Dispose();
}

Hi Tahir,

thanks for the help. I didn´t think that I would have to user another stream as an output stream. Since I am not the one opening the source stream, I cannot close it.

I now use a “SaveStream” to save the result, set the sourcestream length to 0 and after that copy the savestream over the sourcestream.

That helped, thank for the hint.

Best regards

Stefan

Hi Stefan,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.