简体   繁体   中英

How do I create a dropdown list in a word doc that is using a template?

I am trying to create a dropdown in a word doc that is generated using a template (this is all done through a button click in access). When the code is run, it stops at the line for creating the dropdown and gives the following error:

Run-time error '445': Object doesn't support this action

I've narrowed down the problem to this line:

Set doc = oWord.Documents.Add(strWordTemplate)

The dropdown appears no problem when 'strWordTemplate' is removed from 'Add()'. That only gives me a blank document with the dropdown though. How can I place a dropdown in the document generated via the template?

strWordTemplate is a file location for a calendar template for word. TemplatePath is a global string constant to where the word templates are held. Ideally, I would be placing the same dropdown list (same values) in each cell of the calendar, but I want to figure out how to get both the template and the dropdown to show up in the same document first

Public Sub MakeCalendar()
Dim strWordTemplate As String
Dim oWord As Object
Dim doc As Word.Document

    'Open a Word Doc With the Template
    Set oWord = CreateObject("Word.application")
    oWord.Visible = False
    oWord.DisplayAlerts = False

    strWordTemplate = TemplatePath & "Calendar.dot"
    Set doc = oWord.Documents.Add(strWordTemplate) 'template is added here

    doc.ContentControls.Add wdContentControlDropdownList 'having the template added causes this line to fail

    'Show the Word Doc
    oWord.DisplayAlerts = True
    oWord.Visible = True

End Sub

Tried your (modified) code from excel as I am consider myself Zero in Access (more Accurately i used to run from Access). it works and hope will address your issue as below在此处输入图片说明

and the Code is self explantory

Public Sub MakeCalendar()
Dim strWordTemplate As String
Dim oWord As Object
Dim doc As Word.Document

Dim TemplatePath As String
Dim Tbl As Table, cl As Cell, Rw As Row
'Modify/Delete to your requirement, but take care that last slash ("\") is in place in the path
'it is the most probable cause of error in your code
'It in first place it failed to open the template, but does not give alerts
'as DisplayAlerts set to false and then gives eroor 424 on next line
TemplatePath = "C:\users\user\desktop\"

    'Open a Word Doc With the Template
    Set oWord = CreateObject("Word.application")
    oWord.Visible = True                     ' modify to your choice
    oWord.DisplayAlerts = True               ' modify to your choice

    strWordTemplate = TemplatePath & "Calendar.dotx"
    Set doc = oWord.Documents.Add(strWordTemplate) 'template is added here
    Set Tbl = doc.Tables(1)                      ' Modify to your requirement

    For Each Rw In doc.Tables(1).Rows
    For Each cl In Rw.Cells
    cl.Range.ContentControls.Add wdContentControlDropdownList
    Next cl
    Next Rw
     ' Or use your original code
    'doc.ContentControls.Add wdContentControlDropdownList 'having the template added causes this line to fail

    'Show the Word Doc
    oWord.DisplayAlerts = True
    oWord.Visible = True

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM