我正在尝试创建一个打开Word并创建表格的Excel宏。我在Word中创建了一个宏来创建表并将其复制到Excel宏中。当我运行它时,我收到此错误:
运行时错误' 450' 错误的参数数量或无效的属性赋值
在excel是执行MS Word宏(在Word中记录)的那一点:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
编辑:我已为Word doc
设置了参考 ' Declare the variables
Dim WordApp As Object
Dim WordDoc As Object
Dim wksSource As Worksheet
' Assign the active worksheet to an object variable
Set wksSource = ActiveSheet
' Create an instance of the Word application
Set WordApp = CreateObject("Word.Application")
' Make the Word application visible
WordApp.Visible = True
' Open the specified Word document and assign it to an object variable
Set WordDoc = WordApp.Documents.Open("C:\Users\Briet\Documents\PAJ\labels.dotm")
答案 0 :(得分:0)
确保您已在Excel VBA项目中添加了对Word对象的引用。
这对我有用:
Sub Tester()
Dim wd As Word.Application
Set wd = GetObject(, "word.application") 'get reference to open Word document
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
End Sub
请注意,由于Selection
是原生Excel对象,因此您需要对其进行限定,以表明您正在引用 Word中的选择
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range