MS Word VBA:从word控制excel工作簿 – Object required error

我正在MS Word中创build一个MACRO,它需要能够(基本上)将表格的内容复制并粘贴到excel中。

免责声明:这可能看起来太复杂了; 然而,这种方法是必需的,因为它是一个更复杂的处理设置。

长话短说,我循环遍历文档中的每个表格,然后遍历表格中的每个单元格,并将文本放入Excel表单中的相应单元格。

我有这些excel对象的声明:

'Objects Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlRange As Excel.Range 

在我的循环底部,我有以下代码:

 xlBook.Worksheets(x).Activate Set xlRange = xlBook.ActiveSheet.Range(Chr(65 + x) & y) xlRange.Text = tString 

最后一行是抛出一个“需要对象”的错误。 variableststring被定义为一个string,并在循环的前面设置。

完整的代码:

 Sub CopyTablesToExcel() 'Constants Const COLUMN_INDEX = 1 Const ROW_INDEX = 2 'Ints Dim x As Integer, y As Integer, z As Integer 'Counters Dim numTables As Integer 'Number of tables in the word file Dim numSheets As Integer 'Number of sheets in the excel file Dim LastCell(1 To 2) As Integer 'Used to keep track of the last cell of a newly created excel table Dim map() As Integer 'Holds a map of the table columns 'strings Dim xlBookName As String 'Name of the excel workbook Dim tString As String 'Temporary string 'Objects Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlRange As Excel.Range 'Initialize Set xlBook = xlApp.Workbooks.Add numSheets = xlBook.Worksheets.count numTables = ActiveDocument.Tables.count 'Save the new book xlBookName = InputBox("Enter the ticker symbol:") xlBook.SaveAs FileName:=xlBookName 'Show the file? xlApp.Visible = True 'Make sure there are enough sheets If numSheets < numTables Then For x = 1 To (numTables - numSheets) xlBook.Worksheets.Add numSheets = numSheets + 1 Next End If 'Cycle through every table in the document and paste it to the worksheet For z = 1 To numTables 'Cycle through tables 'Keep track of the last cell in the table LastCell(COLUMN_INDEX) = ActiveDocument.Tables(z).Columns.count LastCell(ROW_INDEX) = ActiveDocument.Tables(z).rows.count For x = ActiveDocument.Tables(z).rows(ActiveDocument.Tables(z).rows.count).Cells.count To 1 Step -1 'Cycle through columns 'Used selections to support horizontally merged cells ActiveDocument.Tables(z).rows(ActiveDocument.Tables(z).rows.count).Cells(x).Select Selection.SelectColumn For y = Selection.Cells.count To 1 Step -1 'Cycle through cells tString = Selection.Cells(y).Range.Text 'Move the content into excel xlBook.Worksheets(x).Activate Set xlRange = xlBook.ActiveSheet.Range(Chr(65 + x) & y) Debug.Print Chr(65 + x) & y 'Prints as expected xlRange.Text = tString Next Next Next End Sub 

我相信这是因为MACRO无法正确设置xlRange。 debug.print的输出是正确的,是“A#”的格式。

编辑:

 If Not xlRange Is Nothing Then xlRange.Text = tString 'Still errors End If 

以上将评估为真,但仍在标记线上抛出错误

我看到两件事情:

.Text是只读属性。 我期望在这一行上的“无法设置范围对象的文本属性”错误:

 xlRange.Text = tString 

改成:

 xlRange.Value = tString 

此外,您的范围分配可能是错误的。 我不知道你为什么要做CHR(65)而不是简单的“A”,但是问题是这样的:

 Set xlRange = xlBook.ActiveSheet.Range(Chr(65 + x) & y) 

在这里, x 添加65 ,然后Chr函数返回任何可能引发错误的结果。 大于25的x值很可能会引起错误,因为Chr(65 + x)不计算为有效的范围地址。

既然你在评论中澄清说你打算这样做(例如,“A”+ 1 =“B”等),如果没有其他原因比看起来更清晰,以较不明确的方式利用Excel对象模型:

 Set xlRange = xlBook.ActiveSheet.Range("A" & y).Offset(,x)