我正在尝试在使用模板生成的Word文档中创建一个下拉菜单(这全部通过在访问中单击按钮来完成)。运行代码时,它会停在创建下拉菜单的行上,并显示以下错误:
运行时错误“ 445”: 对象不支持此操作
我将问题缩小到这一行:
设置doc = oWord.Documents.Add(strWordTemplate)
从'Add()'中删除'strWordTemplate'时,下拉列表没有问题。但是,这只给了我带有下拉菜单的空白文档。如何在通过模板生成的文档中放置一个下拉菜单?
strWordTemplate是单词日历模板的文件位置。 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
答案 0 :(得分:1)
尝试了来自excel的(修改后的)代码,因为我认为自己在Access中为零(更准确地说,我以前是从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