Replies: 2   Views: 2461
Dropdown in a word template, how to set the value
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 30-07-2017 - 09:54


MS Word dropdowns don't include a specific tag to set the active value. The first item w:listEntry in the w:ddList of the XML is the selected one; for example:

    <w:listEntry w:val="Two"/>
    <w:listEntry w:val="One"/>
    <w:listEntry w:val="Three"/>
    <w:listEntry w:val="Four"/>

Value 'Two' will be the value of the DropDown.

If you want to change the value of DropDowns, you can use DOCXPath to move the listEntry of the form element to the first position. For example we create a DOCX with a DropDown and value 'Two' is the default value (phpdocx reorders the elements when adding the DropDown to set the selected one in the first position):


require_once 'classes/';

$docx = new CreateDocx();

$selectOptions = array(
    'selectOptions' => array('One', 'Two', 'Three', 'Four'), 
    'fontSize' => 14,
    'color' => 'C90000', 
    'bold' => true, 
    'underline' => 'double', 
    'font' => 'Algerian', 
    'defaultValue' => 1);
$docx->addFormElement('select', $selectOptions);


Using DOCXPath you can move the listEntry. For example, if we want to set the third listEntry of the first DropDown as the default value, you can run this code:


require_once 'classes/';

$docx = new CreateDocxFromTemplate('output_template.docx');

$referenceNodeFrom = array(
    'customQuery' => '//w:ddList[1]/w:listEntry[3]',

$referenceNodeTo = array(
    'customQuery' => '//w:ddList[1]/w:listEntry[1]',

$docx->moveWordContent($referenceNodeFrom, $referenceNodeTo, 'before');


This code move the third listEntry before the first listEntry. As you can use any XPath query, you can query the element to be moved by position as we do in the previous code or by the text content of the w:val attribute.