【问题标题】:Run time error when running an Excel Macro within MS Access在 MS Access 中运行 Excel 宏时出现运行时错误
【发布时间】:2016-07-10 12:38:26
【问题描述】:

这是我的第一篇文章,我对编程真的很陌生。

我目前卡在图形构建时停止的一行代码。 基本上我有一个数据库/访问,我有一个功能,我点击按钮并从特定位置打开一个文件。然后它会打开 Excel 并开始运行我的代码,然后停止。

下面是一个sn-p,它卡在下面一行

ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "M³ 每月"

Range("A2:M6").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("LCLSPENDGRAPH!$A$2:$M$6")
ActiveChart.SetElement (msoElementDataTableShow)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "M³ Per Month"
Selection.Format.TextFrame2.TextRange.Characters.Text = "M³ Per Month"
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).Font
    .BaselineOffset = 0
    .Bold = msoTrue
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(0, 0, 0)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike

`

任何建议将不胜感激。

亲切的问候 JDogg

【问题讨论】:

  • 还忘了提到宏/代码只在 Excel 中可以正常工作,但是当我将它嵌入到 Access 中时它不起作用?
  • 从 Access 自动化 Excel 时,您需要获取对 Excel 应用程序对象的引用,并将其与对 Excel 对象的任何引用一起使用。此外,在 Access VB 项目中添加对 excel 对象模型的引用。
  • 感谢 Tim,我在 Access Vb 中添加了 Excel 对象模型。但我怀疑代码不太对劲。
  • Access 中没有 Range 对象 - 需要使用 Excel 应用程序对象引用进行限定:例如 objExcel.ActiveSheet.Range(...)。对于 Excel 而不是 Access“拥有”的对象的任何其他引用也是如此。 stackoverflow.com/questions/5729195/…

标签: excel vba


【解决方案1】:

如果您想从 Access 控制 Excel,您基本上有 2 个选项。请尝试以下 2 个脚本,并在回复中提出其他问题...

‘EARLY BINDING
Option Compare Database
Option Explicit ' Use this to make sure your variables are defined

' One way to be able to use these objects throughout the Module is to Declare them
' Here and not in a Sub

Private objExcel As Excel.Application
Private xlWB As Excel.Workbook
Private xlWS As Excel.Worksheet

Sub Rep()

Dim strFile As String

strFile = "C:\Users\Excel\Desktop\YourExcelFile.xls"

' Opens Excel and makes it Visible
Set objExcel = New Excel.Application
objExcel.Visible = True

'Opens up the Workbook
Set xlWB = objExcel.Workbooks.Open(strFile)

'Sets the Workseet to the last active sheet - Better to use the commented version and use the name of the sheet.
Set xlWS = xlWB.ActiveSheet
'Set xlWS = xlWB("Sheet2")

With xlWS ' You are now working with the Named file and the named worksheet


End With

'Do Close and Cleanup
End Sub


 
‘LATE BINDING
Sub ControlExcelFromAccess()

' No reference to a type library is needed to use late binding.
' As long as the object supports IDispatch, the method can
' be dynamically located and invoked at run-time.

' Declare the object as a late-bound object
  Dim oExcel As Object
  Dim strFile As String

  strFile = "C:\Users\Excel\Desktop\YourExcelFile.xls"

  Set oExcel = CreateObject("Excel.Application")

' The Visible property is called via IDispatch
  oExcel.Visible = True

  Set xlWB = oExcel.Workbooks.Open(strFile)

'Call Ron's code here . . .

Set oExcel = Nothing

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2017-08-01
    相关资源
    最近更新 更多