0

Plain and simply, I have a c:\dropdowntest.docx file that has nothing but one single dropdown list. The dropdown list has two options, "Option1" and "Option2". The file is saved so that "Option1" is selected. How do I select the "Option2" by using the Open XML SDK in C#?

Here's a start for the code, so I'm for example able to refer to the dropdown list:

private static void LoopElements(WordprocessingDocument wordDocument)
    {
        List<SdtElement> sdtelements = wordDocument.MainDocumentPart.Document.Descendants<SdtElement>().ToList();
        foreach (var contentcontrol in sdtelements)
        {
            var listitems = contentcontrol.Descendants<ListItem>().ToList();
            if (listitems.Count > 0)
            {
                SdtRun xStdRun = (SdtRun)contentcontrol;

1 Answers1

3

Here is how you can replace the previously selected option with the last one from listitems:

private static void LoopElements(WordprocessingDocument wordDocument)
{
    List<SdtElement> sdtelements = wordDocument.MainDocumentPart.Document.Descendants<SdtElement>().ToList();
    foreach (var contentcontrol in sdtelements)
    {
        var listitems = contentcontrol.Descendants<ListItem>().ToList();
        if (listitems.Count > 0)
        {
            SdtRun xStdRun = (SdtRun)contentcontrol;
            SdtContentRun xStdContentRun = xStdRun.SdtContentRun;
            Text xStdContentText = xStdContentRun.GetFirstChild<Run>().GetFirstChild<Text>();
            xStdContentText.Text = listitems.Last().DisplayText;
        }
    }
}

Also just in case, you should probably check these answers as well, they address things like what if you don't have a selected option (there is no Run element in SdtContentRun), or what if you have SdtBlock instead of SdtRun, etc.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
  • @AaronQueenan what tool requires LastValue? I've tried few libraries and none has such requirement. Let me know so that I can see if your change should stay or if the problem is with the tool itself. – Mario Z Oct 24 '20 at 14:30
  • @AaronQueenan according to specification: "If this structured document tag is not mapped to XML using the dataBinding element (§2.5.2.6), then this attribute shall be ignored." In other words, this is specific to only content controls that have XML mapping. – Mario Z Oct 25 '20 at 03:44
  • @AaronQueenan also this attribute defines a specific behavior that a tool or application should do in case this new text of ours doesn't exist in mapped XML. – Mario Z Oct 25 '20 at 03:46
  • @AaronQueenan in other words, I'm afraid your usage of this attribute looks like a workaround to some other problem. The content controls that have XML mapping should be updated indirectly, through that bound XML. – Mario Z Oct 25 '20 at 03:48
  • @AaronQueenan can you send me your DOCX file with this element so that I can take a look at it? – Mario Z Oct 25 '20 at 04:13
  • you're right. I looked closer at the process and it wasn't Microsoft Word adding LastValue, it was Aspose. Aspose then ignored the text and relied on LastValue. – Aaron Queenan Oct 26 '20 at 11:42