Search and replace in a file – in memory
Home › Forums › Open-Xml-PowerTools › Search and replace in a file – in memory
Tagged: merged document, Search and replace
This topic contains 2 replies, has 2 voices, and was last updated by Eric White 8 years, 2 months ago.
-
AuthorPosts
-
September 1, 2016 at 5:43 pm #3728
I am trying to replace text in a template document in memory only, and it is not working.
For example the following will work (if I write the file to disk before doing the search and replace)
using (WordprocessingDocument finalDoc = WordprocessingDocument.Open(destDocxFi.FullName, true)) { TextReplacer.SearchAndReplace(newDoc, "<TitlePageGroupAndCode>", _caTitle, true); TextReplacer.SearchAndReplace(newDoc, "<TitlePageBargainingAgentName>", _bargainingAgentName, true); TextReplacer.SearchAndReplace(newDoc, "<TitlePageGroupName>", _groupName, true); TextReplacer.SearchAndReplace(newDoc, "<TitlePageExpiryDate>", _expiryDate, true); TextReplacer.SearchAndReplace(newDoc, "<CopyrightBargainingAgentName>", _bargainingAgentName, true); }
However if I try to simply use the document in-memory as follows, it does not work:
WmlDocument doc = HtmlToWmlConverter.ConvertHtmlToWml(defaultCss, usedAuthorCss, userCss, html, settings, null, null); //doc.SaveAs(destDocxFi.FullName); //doc = null; // grab our template based on language. var templatePath = Server.MapPath(String.Format("~/Content/{0}", 1 == 1 ? "CATemplate_English.docx" : "CATemplate_French.docx")); var templateDoc = new WmlDocument(templatePath); templateDoc.SearchAndReplace("<TitlePageGroupAndCode>", _caTitle, true); templateDoc.SearchAndReplace("<TitlePageBargainingAgentName>", _bargainingAgentName, true); templateDoc.SearchAndReplace("<TitlePageGroupName>", _groupName, true); templateDoc.SearchAndReplace("<TitlePageExpiryDate>", _expiryDate, true); templateDoc.SearchAndReplace("<CopyrightBargainingAgentName>", _bargainingAgentName, true); List<Source> sources = new List<Source>(); sources.Add(new Source(templateDoc, true)); sources.Add(new Source(doc, false)); var newDoc = DocumentBuilder.BuildDocument(sources); //newDoc.SaveAs(destDocxFi.FullName); //newDoc = null; sources = null;
Any thoughts?
I have also tried to do the search and replace on the merged document, with the same issue: no text is replaced.
- This topic was modified 8 years, 2 months ago by gamartin.
September 1, 2016 at 6:12 pm #3730OK I was able to get this to work in memory. I guess the behaviour when you save the file is different than when you just work in memory.
I if do it thusly it works:
// open our template and do our quick search and replace var templateDoc = new WmlDocument(templatePath); templateDoc = TextReplacer.SearchAndReplace(templateDoc, "<TitlePageGroupAndCode>", _caTitle, true); templateDoc = TextReplacer.SearchAndReplace(templateDoc, "<TitlePageBargainingAgentName>", _bargainingAgentName, true); templateDoc = TextReplacer.SearchAndReplace(templateDoc, "<TitlePageGroupName>", _groupName, true); templateDoc = TextReplacer.SearchAndReplace(templateDoc, "<TitlePageExpiryDate>", _expiryDate, true); templateDoc = TextReplacer.SearchAndReplace(templateDoc, "<CopyrightBargainingAgentName>", _bargainingAgentName, true);
Cheers,
Garth
September 12, 2016 at 5:57 am #3768Great, glad you got it working.
Check out this MSDN article:
https://msdn.microsoft.com/en-us/library/ee945362(v=office.11).aspx
Cheers, Eric
-
AuthorPosts
You must be logged in to reply to this topic.