I am using c# to create a personalized a word (.docx) document, since the document have a lot of textboxes doing it just by xml, or modifying words, is giving me terrible headache.
So to avoid this I had the idea (up to you to judge if is bad or good) of copy the full template and then substitute the document.xml of the resulting word. this is the fragment of code that does that:
string pathTemplate = Const.MypathString + Const.MypathtoTemplate + @"\TarjetaClienteTemplate.docx";
string file = Const.MypathString + Const.MypathtoTemplate + @"\test.docx";
Microsoft.Office.Interop.Word.Application App = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document template = App.Documents.Add(pathTemplate);
App.ActiveDocument.SaveAs(file);
App.Quit(); ="http://ift.tt/JiuBoE";
XmlDocument xmlStartPart = new XmlDocument();
xmlStartPart.LoadXml(xml());
Package pkgDoc = null;
pkgDoc = Package.Open(file, FileMode.Create, FileAccess.ReadWrite);
Uri uri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart partDOCXML = pkgDoc.CreatePart(uri,"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
StreamWriter streamStartPart = new StreamWriter(partDOCXML.GetStream(FileMode.Create, FileAccess.Write));
xmlStartPart.Save(streamStartPart);
streamStartPart.Close();
pkgDoc.CreateRelationship(uri, TargetMode.Internal,"http://ift.tt/1ePIJBm", "rId1");
pkgDoc.Flush();
pkgDoc.Close();
The problem that is giving the code is that this fragment of code pkgDoc = Package.Open(file, FileMode.Create, FileAccess.ReadWrite); give me an error " the process cannot open the file because other program has it open".
I don't understand this error, the application is closed after it copied the file and there is no other program (at least called by the code) that has it open. Any Idea?
No comments:
Post a Comment