Purpose: To update the replacement data template data by using Word to create a template and to use the method of modifying the field.
Tools: OpenXML SDK
Puzzle 1:openxml SDK does not have an API to refresh domain information, you need to open Word press F9 refresh
Workaround:
The OPENXML SDK tool allows you to observe the XML structure of the domain as follows, each field is labeled <fldcar fldchartype=begin > Start,<instrtext> record field code,
<fldcar fldchartype=separate > Back <text> Show field results ( This section determines the final result that the field shows in Word, and you can modify this part of the value, Reach no need to F9 refresh the update field in Word ),
<fldcar fldchartype=end> ends the current domain.
The code is as follows
/// <summary> ///Update all domains under this node/// </summary> /// <param name= "Me" >document nodes to update such as documents, body, etc.</param> /// <param name= "values" >about key-value pairs for domain Information</param> Public voidUpdatefileds (openxmlelement me, dictionary<string,string>values) { if(me.) HasChildren) {IEnumerable<OpenXmlElement> Childrens =me. childelements; intCount =0; BOOLHasfileds =false; stringFieldcode=""; intMark =0; foreach(Openxmlelement itemopenxmlelementinchchildrens) {Count++; varitemchildelements=itemopenxmlelement.childelements; if(itemchildelements.count==0) Continue; if(Itemchildelements.oftype<fieldchar> (). FirstOrDefault (p = p.fieldchartype = = fieldcharvalues.begin)! =NULL) {hasfileds=true; Continue; } if(hasfileds) {if(Itemchildelements.oftype<fieldchar> (). FirstOrDefault (p = p.fieldchartype = = fieldcharvalues.end)! =NULL) {hasfileds=false; Fieldcode=""; Continue; } if(Itemchildelements.oftype<fieldchar> (). FirstOrDefault (p = p.fieldchartype = = fieldcharvalues.separate)! =NULL) {Mark= count+1; Continue; } if(Mark = = Count && itemchildelements.oftype<text> (). FirstOrDefault ()! =NULL) { stringValue =""; Values. TryGetValue (Fieldcode, outvalue); Text Text1=NewText (); Text1. Text=value; Itemopenxmlelement.removeallchildren (); Itemopenxmlelement.append (Text1); Continue; } varInstrtext = itemopenxmlelement.childelements.oftype<fieldcode>(). FirstOrDefault (); if(Instrtext! =NULL) { foreach(stringKeyinchvalues. Keys) {if(InstrText.Text.IndexOf (" "+ key +" ") == -1) {Fieldcode=""; } Else{Fieldcode=key; } } } } Else{updatefileds (itemopenxmlelement, values); } } } }
Use the OPENXML SDK to manipulate Word to refresh domain information