Word cannot refresh domain information by using the OPENXML SDK operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.