Vba 未定义用户定义类型-控制Excel中的Word
我正试图做一些相对简单的复制和粘贴从Excel 2007到Word 2007。我浏览了这个网站和其他网站,并一直挂在同一件事上-下面代码的第三行一直给我“用户类型注释定义”错误消息。我真的很困惑,因为我刚刚从另一个解决方案中提出了这个问题(我试图提出的其他解决方案也有类似的问题)。有人能告诉我是什么导致了这个错误,为什么Vba 未定义用户定义类型-控制Excel中的Word,vba,excel,ms-word,excel-2007,Vba,Excel,Ms Word,Excel 2007,我正试图做一些相对简单的复制和粘贴从Excel 2007到Word 2007。我浏览了这个网站和其他网站,并一直挂在同一件事上-下面代码的第三行一直给我“用户类型注释定义”错误消息。我真的很困惑,因为我刚刚从另一个解决方案中提出了这个问题(我试图提出的其他解决方案也有类似的问题)。有人能告诉我是什么导致了这个错误,为什么 Sub ControlWord() ' **** The line below gives me the error **** Dim appWD As Word.Applic
Sub ControlWord()
' **** The line below gives me the error ****
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.12")
appWD.Visible = True
'Find the last row with data in the spreadsheet
FinalRow = Range("A9999").End(xlUp).Row
For i = 1 To FinalRow
' Copy the current row
Worksheets("Sheet1").Rows(i).Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs Filename:="File" & i
' Close this new word document
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
这一回答是在一篇评论中提到的
为了解决这个问题,必须将Word对象库引用添加到项目中
在Visual Basic编辑器中
,选择工具
,然后选择引用
,并向下滚动列表,直到看到Microsoft Word 12.0对象库
。选中该框并点击Ok
从那时起,当您键入Word时,应启用自动完成功能。
以确认参考设置正确。根据
- 使用非类型化变量:
Dim appWD as Object appWD = CreateObject("Word.Application")
- 通过
将对Tools->References…
Microsoft Word对象库的引用添加到VBA项目中,然后创建一个类型化变量并对其进行初始化: 或
Dim appWD作为Word.Application Set appWd=New Word.Application
相当于这里的CreateObject
,它只引入了代码冗余New
键入的变量将为您提供自动完成功能。您需要在项目中设置对Word库的引用(您可以在VB编辑器中的“工具>>引用”下进行此操作),谢谢Tim-以前从未这样做过。此修复可能重复!我们现在使用Microsoft Word 16.0对象库。谢谢!伟大的解决方案
Dim appWD as New Word.Application
Dim appWD as Word.Application
<...>
Set appWd = New Word.Application