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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:02:32+00:00 2026-06-03T01:02:32+00:00

Using the MS OpenXml Sdk I have been able to copy a templatestream to

  • 0

Using the MS OpenXml Sdk I have been able to copy a templatestream to a resultstream and append dynamic text(w.p>>w.r>>w.t) at the end of the body using the following code:

var templateStream = File.OpenRead(templatePath);
templateStream.CopyTo(resultStream);

using (var resultPackage = WordprocessingDocument.Open(resultStream, true))
{
     var document = resultPackage.MainDocumentPart.Document;

     var body = document.Body;

     // Add new text.
     var para = body.AppendChild(new Paragraph());
     var run = para.AppendChild(new Run());
     run.AppendChild(new Text(firstName));
     document.Save();
}

My next logical step was to then replace the innertext of a textbox in the resultStream with the firstName as in the code below.

// replacing code in using statement from above
var document = resultPackage.MainDocumentPart.Document;
var textbox = document.Descendants<TextBox>().First();
const string firstNametag = "<<IH.FirstName>>";
if (textbox.InnerText.Contains(firstNametag))
{
   var textboxContent = textbox.Elements<TextBoxContent>().First();
   textboxContent.RemoveAllChildren();
   var paragraph = textboxContent.AppendChild(new Paragraph());
   var run = paragraph.AppendChild(new Run());
   run.AppendChild(new Text(firstName));
}
document.Save();

In the first example and with some additional code the result stream is appropriately serialized to a docx and the firstName is appended to the end of the body when viewed in Word. In the second example though the textbox and its contents remain the same even though further examination in the debugger showed the textboxContent’s children reflecting the changes made above.

I am new to OpenXML development so if there is anything obvious please point it out.

  • 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-06-03T01:02:34+00:00Added an answer on June 3, 2026 at 1:02 am

    Oh wow, I don’t even want to think I understand the whole picture around this but here’s a quick stab at it. Someone with more openXml experience please chime in…

    Turns out when you create a textbox in word on a docx the document.xml file get’s the following markup:

    <w.r>
        <mc.AlertnateContent>
            <mc.Choice Requires="wps">
                <wps:txbx>
                    <w:txbxContent>
                        <w:r>
                            <w.t>
                                Text Goes Here
                            </w.t>
                        </w.r>
                    </w:txbxContent>
                </wps:txbx>
            </mc.Choice>
            <mc.Fallback>
                <v.textbox>
                    <w:txbxContent>
                        <w:r>
                            <w.t>
                                Text Goes Here
                            </w.t>
                        </w.r>
                    </w:txbxContent>
                </v.textbox>
            </mc.Fallback>
        </mc.AlertnateContent>
    </w.r>
    

    Notice the mc.AlternateContent, mc.Choice, and mc.Fallback tags. What the heck are these??? Someone put it this way on a blog article I came across –

    “As I understand it – but don’t take my word as gospel – AlternateContent can
    appear anywhere and provides a mechanism for including enhanced
    functionality if the consuming application can handle it, along with a
    fallback if it can’t.”
    -Tony Jollans – http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f8a5c277-7049-48c2-a295-199d2914f4ba/

    In my case I was only modifying the fallback textbox(v.txbx not wps.txbx) because of my mishap in assuming Resharper was right in asking me to import the DocumentFormat.OpenXml.Vml namespace for my dependency on the TextBox object. Not sure why there isn’t a Textbox definition in one of my already included namespaces, DocumentFormat.OpenXml.Packaging or DocumentFormat.OpenXml.Wordprocessing but that’s beyond the scope of this question. Needless to say, upon realizing this and updating my code to look for the common w.txbxContent for the two I achieved what I wanted to do.

    Here’s the updated code with some refactoring, call the ReplaceTag method in the using statement from the original question and supply a model object instead of a string. Also, use the tagToValueSelector dictionary for convenience.

    private void ReplaceTags(Document document, SomeModel model)
    {
        var textBoxContents = document.Descendants<TextBoxContent>().ToList();
        foreach (var textBoxContent in textBoxContents)
        {
            ReplaceTag(textBoxContent, model);
        }
    }
    
    private void ReplaceTag(TextBoxContent textBoxContent, SomeModel model)
    {
        var tag = textBoxContent.InnerText.Trim();
        if (!tagsTomValues.ContainsKey(tag)) return;
        var valueSelector = tagsTomValues[tag];
        textBoxContent.RemoveAllChildren();
        var paragraph = textBoxContent.AppendChild(new Paragraph());
        var run = paragraph.AppendChild(new Run());
        run.AppendChild(new Text(valueSelector(model)));
    }
    
    // called in the ctor
    private static void IntializeTags(IDictionary<string, Func<SomeModel, string>> dictionary)
    {
        dictionary.Add("<<IH.Name>>", m => string.Format("{0} {1}", m.FirstName, m.LastName));
    }
    

    Happy openXmling 🙂

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

Sidebar

Related Questions

I have created an Excel document using OpenXml SDK 2.0, now I have to
Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
I have a library that generates Word documents using the OpenXML SDK, one of
Using the Open XML SDK I've been successful in programatically finding bookmarks or text
I created a docx document using OpenXML SDK. It contains group content controls to
I'm trying to merge two docx-documents into one docx-document using OpenXML SDK 2.0. The
I'm using the OpenXML SDK to programatically replace some <w:sdt/> elements with chunks of
I'm using version 1.0 of Microsoft's OpenXML SDK to do some basic parsing of
Using the OpenXML SDK, 2.0 CTP, I am trying to programmatically create a Word
(I am working interactively with a WordprocessingDocument object in IronPython using the OpenXML SDK,

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.