我正在尝试在使用模板生成的单词doc中创建一个下拉列表(这都是通过访问中的按钮单击完成的)。代码运行时,它会在创建下拉列表的行中停止,并显示以下错误:
运行时错误'445':对象不支持此操作
我把问题缩小到这条线:
设置doc = oWord.Documents.Add(setWordTemplate)
从'Add()'中删除'strWordTemplate'时,下拉列表没有问题。这只给了我一个带有下拉列表的空白文档。如何在通过模板生成的文档中放置下拉列表?
strWordTemplate是word的日历模板的文件位置。 TemplatePath是一个全局字符串常量,用于保存单词模板。理想情况下,我会在日历的每个单元格中放置相同的下拉列表(相同的值),但我想弄清楚如何让模板和下拉列表首先显示在同一文档中
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
尝试从excel中修改你的(修改过的)代码,因为我认为自己是零访问(更准确地说我曾经从Access运行)。它的工作原理和希望将解决你的问题,如下
而且守则是自我解释的
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