vba 用户定义类型未定义-从 Excel 控制 Word
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11728899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User defined type not defined- controlling Word from Excel
提问by Tommy Z
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?
我正在尝试从 Excel 2007 中进行一些相对简单的复制并粘贴到 Word 2007 中。我浏览了这个网站和其他网站,并一直在关注同一件事——下面代码的第三行不断给我“用户类型注释已定义”错误消息。我真的很困惑,因为我刚刚从另一个解决方案中解决了这个问题(并且在我试图解决的其他解决方案中遇到了类似的问题)。有人可以告诉我是什么导致了错误,为什么?
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
回答by ForceMagic
This answer was mentioned in a comment by Tim Williams.
Tim Williams在评论中提到了这个答案。
In order to solve this problem, you have to add the Word object library reference to your project.
为了解决这个问题,您必须将 Word 对象库引用添加到您的项目中。
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
.
在 中Visual Basic Editor
,选择Tools
thenReferences
并向下滚动列表,直到看到Microsoft Word 12.0 Object Library
。选中该框并点击Ok
。
From that moment, you should have the auto complete enabled when you type Word.
to confirm the reference was properly set.
从那一刻起,您应该在键入时启用自动完成功能Word.
以确认引用设置正确。
回答by ivan_pozdeev
As per What are the differences between using the New keyword and calling CreateObject in Excel VBA?, either
根据Excel VBA 中使用 New 关键字和调用 CreateObject 之间的区别是什么?, 任何一个
use an untyped variable:
Dim appWD as Object appWD = CreateObject("Word.Application")
使用无类型变量:
Dim appWD as Object appWD = CreateObject("Word.Application")
or
或者
Add a reference to
Microsoft Word <version> Object Library
into the VBA project viaTools->References...
, then create a typed variable and initialize it with the VBANew
operator:Dim appWD as New Word.Application
or
Dim appWD as Word.Application <...> Set appWd = New Word.Application
CreateObject
is equivalent toNew
here, it only introduces code redundancy
Microsoft Word <version> Object Library
通过 向 VBA 项目添加一个引用Tools->References...
,然后创建一个类型化变量并使用 VBANew
运算符对其进行初始化:Dim appWD as New Word.Application
或者
Dim appWD as Word.Application <...> Set appWd = New Word.Application
CreateObject
相当于New
这里,只是引入了代码冗余
A typed variable will give you autocomplete.
一个类型化的变量会给你自动完成。