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 6233519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:18:55+00:00 2026-05-24T10:18:55+00:00

For creating word documents based on data from an SQL database, I’m using Office

  • 0

For creating word documents based on data from an SQL database, I’m using Office Open XML SDK to avoid using interop. This speeds up the process and it eliminates the requirement for a Microsoft office suite installed on the client system.

While this works very well, there is a problem I’m having when replacing certain text in the document. To keep customization of the final document an option, I’ve created a document with some tags in it as a template. This template contains tags such as [TagHere]. Since the tag names should be easy readable, they could be used throughout the document, which is why I’ve surrounded the tag with braces [].

This works quite well, but sometimes, an issue comes up. When you’re typing in a docx document, the text can be split up into multiple tags, even in the same word. A tag like [TagHere] can be split up into

<tag>[</tag><tag>TagHere</tag><tag>]</tag>

When this happens, the replacement won’t work.

Now the docx format has some alternative options to do this kind of operations, such as Content Controls, but these make the process of creating the template more complex. Furtermore, it is not uncommon in these documents to get one row of a table with tags and copy it multiple of times, which would probably break the content tag principle. Hence I’ve chosen to not use this option.

It would be great if someone has a solution to this problem.

  • 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-24T10:18:56+00:00Added an answer on May 24, 2026 at 10:18 am

    instead of typing plain text “taghere”, insert a merge field. (in word, click insert > quick parts > field. choose “mergefield” and type “TagHere” in the “Field name” field.)

    then instead of doing a text find-replace, scan the document for merge fields and set the inner texts.

    class Program
    {
        static void Main(string[] args)
        {
            string document = args[0];
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
            {
                Dictionary<string, string> replaceOperations = new Dictionary<string, string>();
    
                replaceOperations.Add("company", "alex's applications");
                replaceOperations.Add("first_name", "alexander");
                replaceOperations.Add("last_name", "taylor");
                //etc
    
                Replace(wordDoc, replaceOperations);
            }
        }
    
        public static char[] splitChar = new char[] {' '};
        public static void Replace(WordprocessingDocument document, Dictionary<string, string> replaceOperations)
        {
            //find all the fields
            foreach (var field in document.MainDocumentPart.Document.Body.Descendants<SimpleField>())
            {
                //parse the instruction
                string[] instruction = field.Instruction.Value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
    
                //check if it's a merge field, and if so...
                if (instruction[0].ToLower().Equals("mergefield"))
                {
                    //get the field name
                    string fieldname = instruction[1];
    
                    //find the text inside (there will only be one)
                    foreach (var fieldtext in field.Descendants<Text>())
                    {
                        //see if we know what to set this value to
                        string value = replaceOperations.ContainsKey(fieldname) ? replaceOperations[fieldname] : null;
    
                        //if we found the replace value, set the text to this value
                        if (value != null)
                            fieldtext.Text = value;
    
                        //should only be one text inside
                        break;
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built a template-based document generator using the Open XML SDK (1.0) , the
I'm creating word documents from templates (.dot) via Microsoft.Office.Interop.Word . The code below works
I've attempted creating an SQL query which only selects rows from database1.documents which has
I am creating an application that gets thousands of word documents from an Oracle
I am creating a simple word doc, using the openXml SDK. It is working
I'm creating a program to transfer text from a word document to a database.
Using .NET and the Word Interop I am programmatically creating a new Word doc
I am currently in the process of creating API documentation using Microsoft Word 2007
I'm creating a word document programatically using VBA. 1) I have a a string
I'm using C# office ( word ) automation by Microsoft Office 12.0 Object 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.