c# - Word interop; replacing some text with a line break -
i have word document template , want replace placeholder; #address number of lines of text. works except environment.newline gets ignored. how set in line break each new line? should point out in code, addresslines string made of each line in collection seperated comma , standard new line constant.
public static void printaddress(string companyname, list<string> lines, string outputfolder) { // todo set in linebreak each line of address lines.insert(0, companyname); var lineend = "," + environment.newline; var addresslines = string.join(lineend, lines.toarray()); var application = new application(); var path = path.combine(httpcontext.current.server.mappath(appsettings.uploadfolder), sessionobjectsscd.uploadedfile.filename); var document = application.documents.open(path); worddocumentclass.findandreplace(document, "#address", addresslines); object filename = string.format("{0}/{1}.docx", outputfolder, companyname); document.saveas(ref filename); // todo print document application.quit(); } public static void findandreplace(document document, string placeholder, string newtext) { object missingobject = null; object item = wdgotoitem.wdgotopage; object whichitem = wdgotodirection.wdgotofirst; object replaceall = wdreplace.wdreplaceall; object forwards = true; object matchallword = true; object matchcase = false; object originaltext = placeholder; object replacetext = newtext; document.goto(ref item, ref whichitem, ref missingobject, ref missingobject); foreach (range rng in document.storyranges) { rng.find.execute( ref originaltext, ref matchcase, ref matchallword, ref missingobject, ref missingobject, ref missingobject, ref forward, ref missingobject, ref missingobject, ref replacetext, ref replaceall, ref missingobject, ref missingobject, ref missingobject, ref missingobject); } }
i found answer; var lineend = ",\x0b";
next time seek open xml toolkit mentioned in other answer.
c# ms-word office-interop
No comments:
Post a Comment