Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:35:46+00:00 2026-05-26T19:35:46+00:00

I am actually using the library DocX to generate Word document (2007+) from .Net.

  • 0

I am actually using the library “DocX” to generate Word document (2007+) from .Net. What is good is that it can use “docx” template to recreate or update a document.

Problem: When I “AddCustomProperty(…)” it does not update the word document. I actually need to open it and then select all and push F9. I was wondering if there was a way to auto-update the “custom properties” using the DocX library.

To reproduce my problem you can do the following steps:

  1. Open the “sample” available on the DocX project.
  2. Run it once (it will create files in the debug\docs)
  3. Open the invoice template and then add a line (with or without text) and save the file
  4. Re-run the same sample project (it will use the existing template)
  5. Open the invoice result. When you do so, you can see that the table have been created, BUT, all other fields haven’t been updated until you select all and then push CTRL+F9

If anyone has a solution, I would gladly like to ear about it.

(Note: I don’t want the MS Word interop)

Project and samples are available at : http://docx.codeplex.com/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:35:47+00:00Added an answer on May 26, 2026 at 7:35 pm

    The problem is when we use MS Word (I use the 2010 version) and then we modify the template and save it. It changes what the document contains.

    Here’s what we have when we first generate the template using DocX:

    <w:fldSimple w:instr="DOCPROPERTY company_name \* MERGEFORMAT" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:r>
        <w:t>
          <w:rPr>
            <w:b />
            <w:sz w:val="24" />
            <w:szCs w:val="24" />
            <w:color w:val="1F497D" />
          </w:rPr>Company Name</w:t>
      </w:r>
    </w:fldSimple>
    

    And when we edit with Word (add a break line or some text) and we save it, it rewrite the fldSimple to something like that:

    <w:p w:rsidR="006D64DE" w:rsidRDefault="006B25B1">
            <w:r>
              <w:fldChar w:fldCharType="begin" />
            </w:r>
            <w:r>
              <w:instrText>DOCPROPERTY company_name \* MERGEFORMAT</w:instrText>
            </w:r>
            <w:r>
              <w:fldChar w:fldCharType="separate" />
            </w:r>
            <w:r>
            <w:rPr>
               <w:b />
              <w:color w:val="1F497D" />
              <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
      <w:t>Company Name</w:t>
      </w:r>
      ...
      <w:r>
        <w:rPr>
          <w:b />
          <w:color w:val="1F497D" />
          <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
        <w:fldChar w:fldCharType="end" />
      </w:r>
    </w:p>
    

    Instead of waiting someone to fix the issue, I simply tried to do a first draft of implementation. I actually modified the method UpdateCustomPropertyValue(…). I actually added the code of the first foreach. The second foreach was already there and it apply to document created from DocX.

     internal static void UpdateCustomPropertyValue(DocX document, string customPropertyName, string customPropertyValue)
            {
                foreach (XElement e in document.mainDoc.Descendants(XName.Get("instrText", w.NamespaceName))) 
                {
                    string attr_value = e.Value.Replace(" ", string.Empty).Trim();
                    string match_value = string.Format(@"DOCPROPERTY  {0}  \* MERGEFORMAT", customPropertyName).Replace(" ", string.Empty);
    
                    if (attr_value.Equals(match_value, StringComparison.CurrentCultureIgnoreCase))
                    {
                        XNode node = e.Parent.NextNode;
                        bool found = false;
                        while (true)
                        {
                            if (node.NodeType == XmlNodeType.Element)
                            {
                                var ele = node as XElement;
                                var match = ele.Descendants(XName.Get("t", w.NamespaceName));
                                if (match.Count() > 0)
                                {
                                    if (!found)
                                    {
                                        match.First().Value = customPropertyValue;
                                        found = true;
                                    }
                                    else
                                    {
                                        ele.RemoveNodes();
                                    }
                                }
                                else
                                {
                                    match = ele.Descendants(XName.Get("fldChar", w.NamespaceName));
                                    if (match.Count() > 0)
                                    {
                                        var endMatch = match.First().Attribute(XName.Get("fldCharType", w.NamespaceName));
                                        if (endMatch != null && endMatch.Value == "end")
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            node = node.NextNode;
                        }
                    }
                }
    
                foreach (XElement e in document.mainDoc.Descendants(XName.Get("fldSimple", w.NamespaceName))) 
                {
                    string attr_value = e.Attribute(XName.Get("instr", w.NamespaceName)).Value.Replace(" ", string.Empty).Trim();
                    string match_value = string.Format(@"DOCPROPERTY  {0}  \* MERGEFORMAT", customPropertyName).Replace(" ", string.Empty);
    
                    if (attr_value.Equals(match_value, StringComparison.CurrentCultureIgnoreCase))
                    {
                        XElement firstRun = e.Element(w + "r");
                        XElement firstText = firstRun.Element(w + "t");
                        XElement rPr = firstText.Element(w + "rPr");
    
                        // Delete everything and insert updated text value
                        e.RemoveNodes();
    
                        XElement t = new XElement(w + "t", rPr, customPropertyValue);
                        Novacode.Text.PreserveSpace(t);
                        e.Add(new XElement(firstRun.Name, firstRun.Attributes(), firstRun.Element(XName.Get("rPr", w.NamespaceName)), t));
                    }
                }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if anyone can recommend a good library to generate
Actually i am using the AsyncCalls library to execute an Query asynchronously in this
Actually I created a button using the cobogw widget library. But the button does
I am actually using h:selectOneRadio to display items, given to it from f:selectItems tag.
Has anyone using .net actually worked out how to successfully sign a signature to
I'm running a Hadoop job using Hive actually that is supposed to uniq lines
I'm converting an application from the .Net framework to Qt using C++. The application
i'm using dompdf library to render some html content into pdf files. I actually
I am using the GD library to automatically generate a thumbnail version of an
How do you know if the browser is actually using the cached swf RSL/library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.