【发布时间】:2012-07-28 13:23:13
【问题描述】:
我正在尝试从 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
【问题讨论】:
-
您需要在项目中设置对 Word 库的引用(您可以在 VB 编辑器中的 Tools>>References 下进行)
-
感谢蒂姆-以前从未这样做过。
标签: vba excel ms-word excel-2007