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

  • Home
  • SEARCH
  • 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 7677053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:20:12+00:00 2026-05-31T17:20:12+00:00

So, first off here’s my code to open the dotx and create a new

  • 0

So, first off here’s my code to open the dotx and create a new docx copy (of which the copy is then modified). Cut for brevity, but essentially takes 3 params a data table (to make it usable by legacy systems), the UNC path as a string to a template and a UNC path as a string to the output document:

using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(outputPath, true))
{
    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (SimpleField field in docGenerated.MainDocumentPart.Document.Descendants<SimpleField>())
    {
        string mergeFieldName = GetFieldName(field).Trim();

        DataRow[] dr = dtSchema.Select("FieldName = '" + mergeFieldName + "'");

        if (dr.Length > 0)
        {
            string runProperties = string.Empty;
            foreach (RunProperties property in field.Descendants<RunProperties>())
            {
                runProperties = property.OuterXml;
                break;
            }

            Run run = new Run();
            run.Append(new RunProperties(runProperties));
            run.Append(new Text(dr[0]["FieldDataValue"].ToString()));

            field.Parent.ReplaceChild<SimpleField>(run, field);
        }
    }

    docGenerated.MainDocumentPart.Document.Save();
}

What I did initially was take a .dot template and re-save it as a .dotx and crossed my fingers, didn’t work. So instead I tried deleting all merge fields in the .dotx and adding them again. This worked – but it would only find one merge field (as a SimpleField), specifically the last one added before saving the .dotx. Looking further at the template using the open XML productivity tool I can see that all other merge fields are of type w:instrText which is why they’re being ignored.

I’m literally just starting out with OpenXML as we’re looking to replace our current office automation with it so I know very little at this point. Could someone please instruct me a bit further or point me to a good resource? I’ve Google’d around a bit but I can’t find my specific problem. I am trying to put off reading through the whole SDK documentation (I know, I know!) as I need to get a solution put together quickly so am focusing on a single task which is to take our existing .dot templates, convert them to .dotx and just replace merge fields with data to derive a .docx.

Thanks in advance!

  • 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-31T17:20:14+00:00Added an answer on May 31, 2026 at 5:20 pm

    Working with OpenXml – you don’t strictly need to use .dotx for your templates, instead you can make your templates just using DocX straight away. A good resource of learning OpenXML is obviously http://openxmldeveloper.org/ and you can find a good pdf read there.

    Also worth looking at third party API docx.codeplex.com which I am using now for developing server side doc automation solution for my company. See the example http://cathalscorner.blogspot.co.uk/2009/08/docx-v1007-released.html which is similar to your scenario.. merging fields with data..
    Hope this helps..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off here is my code: CREATE FUNCTION unknown_Model () RETURNS TRIGGER LANGUAGE plpgsql
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
this is my first post here. First off, some background, I'm new to C#
First off, here's my code: Sub SimulatePortfolio() Dim lambda As Double Dim num As
First off, here's my code: var variable1 = 10; function f1 (){ alert(variable1); }
First off i'm new here so if I made any mistakes please point them
First off here is my code. IEnumerable<XElement> targetDirectory = from XElement e in workingXmlDocument.Descendants(wixNS
first off here's my current code. <div id=background> <img src=src/1080pTux.png height=100%> </div> #background{ position:fixed;
First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int
Having a bit of CSS problem here. First off, my HTML code: <p> <span

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.