用户定义的类型未定义-从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编辑器
中,选择 Tools
,然后选择 References
,然后向下滚动列表,直到看到 Microsoft Word 12.0 Object Library
.选中该框,然后点击确定
.
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屋!