vb 应用程序定义或对象定义错误

Private Sub export_cmd_Click()
Dim r As Integer, c As Integer
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
Set newxls = CreateObject("excel.application")
Set newbook = newxls.Workbooks.Add
Set newsheet = newbook.Worksheets(1)
If khxxcx_data.Recordset.RecordCount > 0 Then
newxls.Visible = True
For i = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(1, i + 1) = khxxcx_datagrid.Columns(i).Caption
Next i
khxxcx_data.Recordset.MoveFirst
Do Until khxxcx_data.EOFAction
r = khxxcx_data.Recordset.AbsolutePosition
For c = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(r + 1, c + 1) = khxxcx_datagrid.Columns(c)
Next c
khxxcx_data.Recordset.MoveNext
Loop
End If
Set newsheet = Nothing
Set newbook = Nothing
Set newxls = Nothing
newxls.Quit
End Sub
运行后就出现那样的错误“应用程序定义或对象定义错误”,但是还是能把数据导出到EXCEL中
请高手帮帮忙
俺已经搞定了,不过还要谢谢你们,下面是我的答案:
Private Sub export_cmd_Click()
Dim r As Integer, c As Integer
Dim newxls As Variant
Dim newbook As Variant
Dim newsheet As Variant
Set newxls = CreateObject("excel.application")
Set newbook = newxls.Workbooks.Add
Set newsheet = newbook.Worksheets(1)
Set newsheet = newbook.ActiveSheet
If khxxcx_data.Recordset.RecordCount > 0 Then
newxls.Visible = True
For i = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(1, i + 1) = khxxcx_datagrid.Columns(i).Caption
Next i

khxxcx_data.Recordset.MoveFirst
Do Until khxxcx_data.Recordset.EOF
r = khxxcx_data.Recordset.AbsolutePosition
For c = 0 To khxxcx_datagrid.Columns.Count - 1
newsheet.Cells(r + 1, c + 1) = khxxcx_datagrid.Columns(c)
Next c
khxxcx_data.Recordset.MoveNext
Loop
End If
newxls.Quit
Set newxls = Nothing
End Sub

第1个回答  2010-06-28
Select (运行到这里,就出现:应用程序定义的或对象定义的错误) Selection.RowHeight = 88.8 Rows("x+8:x+8").Select Selection.RowHeight = 88.88 Rows("x+8:x+89").Select Selection...
第2个回答  2010-06-28
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
这个excel对象不可用 ,AS后面去掉就行了本回答被网友采纳
第3个回答  2012-07-14
newxls .Selection.RowHeight = 88.8 Rows("x+8:x+8").Select Selection.RowHeight = 88.88 newsheet.Rows("x+8:x+89").Select Selection...
相似回答