MS ACCESS中的运行时错误91

时间:2018-01-25 08:24:47

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

我在创建数据透视表和图表的子项目中打开Excel应用程序,图表将导出为PNG文件。该子被调用3次,打开3个不同的excel文件,每个文件导出一个PNG图像。 对于第一次调用,sub不会产生错误。执行sub的第二次调用时发生运行时错误462。

我在任务管理器中检查了excel应用程序的过程,但任务没有被杀死。我已经研究过如何在不使用shell但是失败的情况下终止进程。

即使我在运行sub的第二次调用之前终止进程,错误仍然存​​在。

这是我的代码

Function Test()
    EXLCRTGRAPH "GRAPH_D"
    EXLCRTGRAPH "GRAPH_W"
    EXLCRTGRAPH "GRAPH_M"
End Function

Sub EXLCRTGRAPH(fName As String) 'imgOut As String, tmplPath As String, fName As String)  ' + image name, template filepath and nmae, chart height and width. fName

Dim imgOut As String: imgOut = "C:\TESAMPM\MonDisk\test"
Dim tmplPath As String: tmplPath = "C:\Users\tsbatch\AppData\Roaming\Microsoft\Templates\Charts"

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet

    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open(CurrentProject.path & "\test\" & fName & ".xlsx")
    Set xlSheet = xlBook.Worksheets(fName)

    Dim rangeRow As String
    Dim source As String
    Dim pvC As PivotCache
    Dim pvT As PivotTable

    xlApp.Visible = False
    xlApp.DisplayAlerts = False
    xlSheet.Activate

    xlSheet.Select
    xlSheet.Range("A1").Select
    xlSheet.Range("C1").End(xlDown).Select
    rangeRow = xlSheet.Range(Selection.Address(xlR1C1)).Row  'the error occurs here

    source = fName & "!R1C1:R" & rangeRow & "C3"

    Set pvC = xlBook.PivotCaches.Create(xlDatabase, source)
    Set pvT = pvC.CreatePivotTable(fName & "!R1C7", "pivot", , xlPivotTableVersion12)

    xlSheet.Select
    xlSheet.Cells(1, 7).Select
    With xlSheet.PivotTables("pivot").PivotFields("“ú•t")
        .Orientation = xlRowField
        .Position = 1
    End With
    With xlSheet.PivotTables("pivot").PivotFields("ƒT[ƒo[ƒhƒ‰ƒCƒu")
        .Orientation = xlRowField
        .Position = 2

    End With
    xlSheet.PivotTables("pivot").AddDataField xlSheet.PivotTables("pivot").PivotFields("‹ó‚«—e—Ê(%)"), "‡Œv / ‹ó‚«—e—Ê(%)", xlSum
    With xlSheet.PivotTables("pivot").PivotFields("ƒT[ƒo[ƒhƒ‰ƒCƒu")
        .Orientation = xlColumnField
        .Position = 1
    End With
    xlSheet.Shapes.AddChart.Select
    xlSheet.ChartObjects(1).Activate
    xlBook.ActiveChart.ApplyChartTemplate (tmplPath & "\" & fName & ".crtx")

    On Error Resume Next
    xlSheet.Shapes("Chart 1").Height = 425.1968503937
    xlSheet.Shapes("Chart 1").Width = 850.3937007874
    xlSheet.Shapes("ƒOƒ‰ƒt 1").Height = 425.1968503937
    xlSheet.Shapes("ƒOƒ‰ƒt 1").Width = 850.3937007874
    On Error GoTo 0

    xlSheet.ChartObjects(1).Chart.Export _
        FileName:=imgOut & "\" & fName & ".png", FilterName:="PNG"
    WAITPROC "0.5"

    rangeRow = ""
    xlApp.DisplayAlerts = True
    xlApp.Visible = True
    xlBook.Close (False)
    xlApp.Quit

    Set pvC = Nothing
    Set pvT = Nothing
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing

End Sub

请忽略“ƒT[ƒo[ƒhƒ‰ƒCƒu”

等符号

此代码行中出现错误

rangeRow = xlSheet.Range(Selection.Address(xlR1C1)).Row

我找不到解决问题的原因,请帮忙。

1 个答案:

答案 0 :(得分:0)

你说:

rangeRow = xlSheet.Range(Selection.Address(xlR1C1)).Row

但rangeRow声明为 String ,而Row是 Long