用户定义类型未定义-从 Excel 控制 Word [英] User defined type not defined- controlling Word from Excel
问题描述
我正在尝试从 Excel 2007 中进行一些相对简单的复制和粘贴到 Word 2007.我浏览了这个网站和其他网站,并且一直在关注同样的事情——下面代码的第三行不断给出我的用户类型注释定义"错误消息.我真的很困惑,因为我刚刚从另一个解决方案中解决了这个问题(并且在我试图解决的其他解决方案中遇到了类似的问题).有人可以告诉我导致错误的原因是什么,为什么?
I am trying to do some relatively simple copy and pasting from Excel 2007 into Word 2007. I've looked through this site and others, and keep getting hung up on the same thing- the third line n the code below keeps giving me the "User type note defined" error msg. I am really confused since I just lifted this from another solution (and had similar issues with other solutions I tried to lift). Could someone please educate me on what is causing the error, and why?
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 对象库引用添加到您的项目中.
In order to solve this problem, you have to add the Word object library reference to your project.
在 Visual Basic Editor
中,选择 Tools
,然后选择 References
并向下滚动列表,直到看到 Microsoft Word 12.0 Object Library代码>.选中该框并点击
Ok
.
Inside the Visual Basic Editor
, select Tools
then References
and scroll down the list until you see Microsoft Word 12.0 Object Library
. Check that box and hit Ok
.
从那一刻起,您应该在键入 Word.
时启用自动完成功能,以确认引用设置正确.
From that moment, you should have the auto complete enabled when you type Word.
to confirm the reference was properly set.
这篇关于用户定义类型未定义-从 Excel 控制 Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!