Solved

I need to change the value of a List Control in a Word Document using C#

Posted on 2010-11-27
3
441 Views
Last Modified: 2013-12-17
using Word = Microsoft.Office.Interop.Word;
            var wordApp = new Word.Application();
            wordApp.Visible = true;
            Word.Documents docs = wordApp.Documents;
            Word.Document doc = docs.Open(@"c:\temp\test3.docx", Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Word.Range range = doc.Range(0, 0);

            foreach (Word.ContentControl word_cc in doc.ContentControls)
            {
                MessageBox.Show(word_cc.Range.Text + "\r\n" + word_cc.Title + "\r\n" + word_cc.Tag);
                //word_cc.Range. = "Female";
                if (word_cc.Title=="Gender")
                {
                    string s1=word_cc.DropdownListEntries.ToString();
                    MessageBox.Show(s1);
                }

            }

            doc.Close(Type.Missing, Type.Missing, Type.Missing);
            wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);




test2.docx
0
Comment
Question by:gswitz
  • 2
3 Comments
 

Author Comment

by:gswitz
ID: 34221928
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 34222521
You need to use the Select method of ContentControlListEntry object.

Here is some VBA macro code.
Dim cc As ContentControl
    Dim ccle As ContentControlListEntry
    
    For Each cc In ActiveDocument.ContentControls
        If cc.title = "Gender" Then
            For Each ccle In cc.DropdownListEntries
                If ccle.Value = "Female" Then
                    ccle.Select
                    Exit For
                End If
            Next ccle
        End If
        Exit For
    Next cc

Open in new window

0
 

Author Closing Comment

by:gswitz
ID: 34222630
foreach (Word.ContentControl word_cc in doc.ContentControls)
            {
                if (word_cc.Title=="Gender")
                {
                    foreach(Word.ContentControlListEntry ddle in word_cc.DropdownListEntries)
                    {
                        if (ddle.Value == "Female") ddle.Select() ;
                    }
                }

            }
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question