Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Adding Text To an Existing Word Document (.docx) results in file corruption

$
0
0

Hi, Im trying to add text to a document copied from one directory to another. Heres my code:

            string dir = Directory.GetCurrentDirectory();
            string filename = dir + "\\templates\\TWord.docx";
            string fn = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+ "Doc1.docx";
            File.Copy(filename, fn, true);
            String txt = "Testing";
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fn, true);
            DocumentFormat.OpenXml.Wordprocessing.Document document = wordprocessingDocument.MainDocumentPart.Document;
            DocumentFormat.OpenXml.Wordprocessing.Body body = document.MainDocumentPart.Document.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Body());
            DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());

            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text(txt));

            wordprocessingDocument.Close();

The file copies fine, and begins the process however when I try to open the file after the run.AppendChild() is executed word gives the error: We're Sorry We Cant Open Doc1 Because we found a problem with its contents"

Details: 

Unspecified error

Location: Part:/word/document.xml,Line:1,column:0


Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>