Share via

Set the dropdown or list item in word document using Openxml c#

nagarjuna aravapalli 26 Reputation points
2021-03-31T00:05:35.693+00:00

How to set the value of a dropdown in word document using C# OpenXML NuGet package.
I am able to get the elements of the document and also the list items of the dropdown but setting the particular item is difficult here. I have tried below code to iterate through the document elements. But its not easy to get the available dropdown list items and select accordingly. Can anyone help here with code to get the dropdown list items and set the value?

       string fileName = @"d://worddoc.docx";
        using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileName, true))
        {
               Document document = wordprocessingDocument.MainDocumentPart.Document;
               var body = document.MainDocumentPart.Document.Body;

                        foreach (var item in body.Descendants<DropDownListFormField>())
                        {
                         //Here I am able to view the contents of the dropdown but not able to set to required item
                        Console.WriteLine("---InnerXml---");
                        Console.WriteLine(item.InnerXml);

                        Console.WriteLine("---OuterXml---");
                        Console.WriteLine(item.OuterXml);

                        }
        }

    Output:
    ---InnerXml---
    <w:result w:val="1" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
    <w:listEntry w:val="&lt;Choose&gt;" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
    <w:listEntry w:val="Option1" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
    <w:listEntry w:val="Option2" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
    <w:listEntry w:val="Option3" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
    <w:listEntry w:val="Option4" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />

    ---OuterXml---
    <w:ddList xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:result w:val="1" />
    <w:listEntry w:val="&lt;Choose&gt;" />
    <w:listEntry w:val="Option1" />
    <w:listEntry w:val="Option2" />
    <w:listEntry w:val="Option3" />
    <w:listEntry w:val="Option4" />
    </w:ddList>

        //Below loop is to get the Dropdownselection item but unfortunately this is also not easy to get the dropdown list items easily but its easy to set the value though.

        foreach (var item in body.Descendants<DropDownListSelection>())
                        {
                                Console.WriteLine(item.OuterXml);
                                item.Val = 2; //setting the dropdown value

                        }
output:
<w:result w:val="1" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />

Appreciate your help on this.

Microsoft 365 and Office | Word | For business | Windows
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ezreal95 1 Reputation point
    2021-05-19T07:26:11.333+00:00

    Alternatively, you can use Spire.Doc to insert a dropdown list content control to Word documents. The code is as follows.

    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    
    namespace InsertDropdownList
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a Document object
                Document doc = new Document();
    
                //Add a section and a paragraph
                Section section = doc.AddSection();
                Paragraph paragraph = section.AddParagraph();
    
                //Add dropdown list content control
                StructureDocumentTagInline sd = new StructureDocumentTagInline(doc);
                paragraph.ChildObjects.Add(sd);
                sd.SDTProperties.SDTType = SdtType.DropDownList;
                SdtDropDownList sddl = new SdtDropDownList();
                sddl.ListItems.Add(new SdtListItem("USA"));
                sddl.ListItems.Add(new SdtListItem("Canada"));
                sd.SDTProperties.ControlProperties = sddl;
                TextRange textRange = new TextRange(doc);
                textRange.Text = sddl.ListItems[0].DisplayText;
                sd.SDTContent.ChildObjects.Add(textRange);
    
                //Save to file
                doc.SaveToFile("DropdownList.docx", FileFormat.Docx2013);
            }
        }
    }
    
    0 comments No comments

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-03-31T08:58:34.233+00:00

    Please try the following code to get and set the value of the drop down list item.

                string filePath = @"D:\test\word\1.docx";  
                using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(filePath, true))  
                {  
                    List<SdtContentDropDownList> sdtelements = wordProcessingDocument.MainDocumentPart.Document.Descendants<SdtContentDropDownList>().ToList();  
                    foreach (var contentcontrol in sdtelements)  
                    {  
                        var listitems = contentcontrol.Descendants<ListItem>().ToList();  
                          
                        
                        foreach (var item in listitems)  
                        {  
                            Console.WriteLine(item.DisplayText+" "+ item.Value);  
                        }  
      
                        listitems[0].DisplayText = "Timon3";  
                        listitems[0].Value = "Timon3";  
                    }  
                }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.