通过Access VBA运行时错误-2147417851运行Excel宏

时间:2016-02-09 21:15:07

标签: excel vba excel-vba access-vba ms-access-2010

我创建了将RecordSet复制到Excel的代码,我希望Excel文件中的宏能够运行。代码完美运行,直到它命中代码运行宏。我一定不能正确地调用应用程序,但无法理解它!

Private Sub Command233_Click()
Dim objXLS As Object
 Dim wks As Object
 Dim rsc As Recordset
 Dim idx As Long
 Set rsc = Me.RecordsetClone
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox ("No Records To Export")
Else
 rsc.MoveLast
 rsc.MoveFirst
 Set objXLS = CreateObject("Excel.Application")
 objXLS.Workbooks.Open FileName:="C:\Comps Macro.xlsm", ReadOnly:=True
 Set wks = objXLS.Worksheets(1)
 For idx = 0 To rsc.Fields.Count - 1
   wks.Cells(1, idx + 1).Value = rsc.Fields(idx).Name
 Next
 wks.Range(wks.Cells(1, 1), wks.Cells(1, rsc.Fields.Count)).Font.Bold = True
 wks.Range("A2").CopyFromRecordset rsc, rsc.RecordCount, rsc.Fields.Count
 objXLS.Visible = True
 objXLS.Run ("Format")
End If

 Set objXLS = Nothing

End Sub

我收到的运行时错误是: 运行时错误'-2147417851(80010105)': 对象'_Application'的方法'运行'失败

1 个答案:

答案 0 :(得分:0)

您必须正确引用Sub或Function。 名为Format的Sub在Workbook或Sheet级定义?

如果在Sheet模块中定义(例如Sheet1):

objXLS.Run ("Sheet1.Format")

如果在工作簿级别:

objXLS.Run ("ThisWorkbook.Format")

希望这有帮助