Add a drop down to a Word document

 

Its surprisingly easy to programatically add a drop down list to Word document

 

$Word = New-Object -Com Word.Application
$word.visible = $true
$template = “c:\test\template.docx”  
$Doc = $Word.Documents.Open($template)
$Doc.Activate()   

$cntrl = [Enum]::Parse([Microsoft.Office.Interop.Word.WdContentControlType], “wdContentControlDropdownList”)

$objCC = $doc.ContentControls.Add($cntrl)

$objCC.DropdownListEntries.Add(“PowerShell”)
$objCC.DropdownListEntries.Add(“Ruby”)
$objCC.DropdownListEntries.Add(“Perl”)

 

Create the COM object for Word and set visible.  Open a template (in this case a blank document) file and activate.

 

Set the type of control you want to add and define the possible entries.

 

It should be possible to set the default text but the method for doing that appears to be very awkward.  I’ll publish that if I work it out.

Advertisements
This entry was posted in Office 2013, Powershell. Bookmark the permalink.

One Response to Add a drop down to a Word document

  1. GCulling says:

    Nice Article – helped with trying to set checkboxes that I’ll be using with Outlook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s