用户定义的类型未定义 - 从Excel控制Word

时间:2022-11-05 08:50:08

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

2 个解决方案

#1


9  

This answer was mentioned in a comment by 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编辑器中,选择“工具”,然后选择“引用”,并向下滚动列表,直到看到Microsoft Word 12.0对象库。选中该框,然后按“确定”。

From that moment, you should have the auto complete enabled when you type Word. to confirm the reference was properly set.

从那一刻起,您应该在键入Word时启用自动完成功能。确认参考设置是否正确。

#2


0  

As per What are the differences between using the New keyword and calling CreateObject in Excel VBA?, either

根据使用New关键字和在Excel VBA中调用CreateObject有什么不同?

  • use an untyped variable:

    使用无类型变量:

    Dim appWD as Object
    appWD = CreateObject("Word.Application")
    

or

要么

  • Add a reference to Microsoft Word <version> Object Library into the VBA project via Tools->References..., then create a typed variable and initialize it with the VBA New operator:

    通过Tools-> References ...将对Microsoft Word Object Library的引用添加到VBA项目中,然后创建一个类型化变量并使用VBA New运算符对其进行初始化:

    Dim appWD as New Word.Application
    

    or

    要么

    Dim appWD as Word.Application
    <...>
    Set appWd = New Word.Application
    
    • CreateObject is equivalent to New here, it only introduces code redundancy
    • CreateObject在这里等同于New,它只引入了代码冗余

A typed variable will give you autocomplete.

键入的变量将为您提供自动完成功能。

#1


9  

This answer was mentioned in a comment by 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编辑器中,选择“工具”,然后选择“引用”,并向下滚动列表,直到看到Microsoft Word 12.0对象库。选中该框,然后按“确定”。

From that moment, you should have the auto complete enabled when you type Word. to confirm the reference was properly set.

从那一刻起,您应该在键入Word时启用自动完成功能。确认参考设置是否正确。

#2


0  

As per What are the differences between using the New keyword and calling CreateObject in Excel VBA?, either

根据使用New关键字和在Excel VBA中调用CreateObject有什么不同?

  • use an untyped variable:

    使用无类型变量:

    Dim appWD as Object
    appWD = CreateObject("Word.Application")
    

or

要么

  • Add a reference to Microsoft Word <version> Object Library into the VBA project via Tools->References..., then create a typed variable and initialize it with the VBA New operator:

    通过Tools-> References ...将对Microsoft Word Object Library的引用添加到VBA项目中,然后创建一个类型化变量并使用VBA New运算符对其进行初始化:

    Dim appWD as New Word.Application
    

    or

    要么

    Dim appWD as Word.Application
    <...>
    Set appWd = New Word.Application
    
    • CreateObject is equivalent to New here, it only introduces code redundancy
    • CreateObject在这里等同于New,它只引入了代码冗余

A typed variable will give you autocomplete.

键入的变量将为您提供自动完成功能。