简体   繁体   中英

Replace merge fields with text with OpenXML SDK 2.0

What is the best way to remove all merge fields from a word 2010 document using openxml sdk 2.0, and replace them with a simple text? I have some difficulties to remove them cleanly. Have tried to remove all Run objects that includes a FieldCode with a "MERGEFIELD" defined, and appended a new Run with my text. But I am missing something crucial since the field seems to stay defined for this element.

Ok, I solved this somehow. I pick out every paragraph that contains a FieldCode that contains the text "MERGEFIELD" and run through every Run, pushing all other types onto a List. When I discover a FieldChar End type, I roll back and replace the four Runs making up the merge field with a single Run containing a Text node. The only problem I have left now is nested fields. Eg Merge field inside an if test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM