简体   繁体   English

偶尔的VBA方法'激活'对象'ChartData'失败

[英]Occasional VBA Method 'Activate' of object 'ChartData' failed

Before I begin, here is some history: 在开始之前,这里有一些历史:

  • Created VBA in Excel to open and read three (3) Excel files (includes itself) and input data into charts/tables/graphs into a PowerPoint presentation. 在Excel中创建VBA以打开和读取三(3)个Excel文件(包括其自身)并将数据输入图表/表格/图表到PowerPoint演示文稿中。 This version runs beautifully. 这个版本运行得很漂亮。 VBA kicked off by a User Form VBA由用户表单启动

  • Modified code to fit a requirement passed down to me. 修改后的代码以满足传递给我的要求。 This one causes the error of VBA Method 'Activate' of object 'ChartData' when loading a graph in one particular slide. 当在一个特定幻灯片中加载图形时,这个导致对象'ChartData'的VBA方法'激活'的错误。 This data is transferred from the sheet that kicks off the VBA. 此数据从启动VBA的工作表传输。

  • I was unable to recreate this error steadily until I started saving the Excel file that kicks off the script when it asked. 我无法稳定地重新创建此错误,直到我开始保存Excel文件,该文件在它询问时启动脚本。 Now I can. 现在我能。

  • NO VBA resides in the powerpoint presentation. 没有VBA驻留在powerpoint演示文稿中。

  • Users testing this experience the error first time around. 测试此用户的用户第一次遇到错误。 I do not. 我不。 However, I do in the further iterations I do after saving the Excel book after either a successful or unsuccessful run. 但是,在成功或不成功运行后保存Excel工作簿之后,我会进行进一步的迭代。

Screen behaviors I've noticed when error occurs: 发生错误时我注意到的屏幕行为:

  • Only happens after I save the Excel that kicked off the procedure and I test the procedure again when trying to re-create error. 只有在我保存启动过程的Excel并且在尝试重新创建错误时再次测试过程后才会发生。

  • PowerPoint presentation becomes the 'activated' application while VBA runs in background 当VBA在后台运行时,PowerPoint演示文稿成为“激活的”应用程序

  • Happens on the same slide and chart (yes, using object labels in PowerPoint). 发生在同一幻灯片和图表上(是的,使用PowerPoint中的对象标签)。

  • When error occurs and I break code, I can NOT close PowerPoint or Excel using the File menu. 当发生错误并且我破坏代码时,我无法使用“文件”菜单关闭PowerPoint或Excel。 I HAVE to use the 'Red X' in the upper right hand corner to close. 我必须使用右上角的“红色X”来关闭。 The ribbons and tabs are also unusable (do not react to a clicking event). 色带和标签也无法使用(不会对点击事件做出反应)。 Microsoft does ask the Save option. Microsoft确实询问“保存”选项。

What I've tried: 我尝试过的:

  • Walking through code and explicitly closing objects after they've been opened and are not required. 遍历代码并在对象打开后显式关闭它们并且不是必需的。
  • Varying the placement of the ScreenUpdating, etc. Application processes 改变ScreenUpdating等的应用程序流程的位置

Here is the function where it trips. 这是它旅行的功能。 It trips up at trpChartData.Activate for a particular graph (which is shapeName): 它在特定图形(即shapeName)的trpChartData.Activate上跳闸:

Function insGraphInfo(ByVal numOfSlide As Integer, ByVal shapeName As String, ByVal cellToMod As String, ByVal valToIns As Variant) As Variant

'Inserts data into a CHART TYPE graph
On Error GoTo ERR_INS_GRAPH

    Dim trpChart As PowerPoint.Chart
    Dim trpChartData As ChartData
    Dim trpWkBk As Excel.Workbook
    Dim trpChartSheet As Excel.Worksheet
    Dim errString As String



    Set oPPTSlide = oPPTFile.Slides(numOfSlide)

       With oPPTSlide
            .Select
       End With

    Set oPPTShape = oPPTFile.Slides(numOfSlide).Shapes(shapeName)
    Set trpChart = oPPTShape.Chart
    Set trpChartData = trpChart.ChartData

    Debug.Print "Activating: " & shapeName & " in slide number: " & numOfSlide

    errString = "Activating: " & shapeName & " in slide number: " & numOfSlide

    trpChartData.Activate

    Debug.Print shapeName & " activated."

    errString = shapeName & " activated."

    errString = "Setting Workbook and Worksheet Objects"

    Set trpWkBk = trpChartData.Workbook
    Set trpChartSheet = trpWkBk.Worksheets(1)

    errString = "Inserting Value into appropriate cell)"
    With trpChartSheet
        .Range(cellToMod).Value = valToIns
    End With

    insGraphInfo = valToIns

    errString = "Refreshing Chart."
    With oPPTShape 'Refreshes
        .Chart.ChartData.Activate
        .Chart.ChartData.Workbook.Close
        .Chart.Refresh
    End With

    Set trpWkBk = Nothing
    Set oPPTSlide = Nothing
    Set oPPTShape = Nothing

    Exit Function

ERR_INS_GRAPH:

    MsgBox "An error occurred while: " & errString
    Resume Next

End Function

Excel and PowerPoint are created by two different teams of developers. Excel和PowerPoint由两个不同的开发团队创建。

PowerPoint.Chart is not the same as Excel.Chart PowerPoint.ChartExcel.Chart不同

Yes, they look the same and you would think that you have the same level of access to their properties, but that is where you would be wrong. 是的,它们看起来一样,您会认为您对其属性具有相同级别的访问权限,但这就是您错误的地方。 The PowerPoint version is very limited. PowerPoint版本非常有限。

Anyway, as far I can tell, you went wrong when you declared 无论如何,据我所知,你宣布时出错了

Dim trpChartData As ChartData

Instead of 代替

Dim trpChartData As PowerPoint.ChartData

As Rachel pointed out, 正如雷切尔指出的那样,

trpChartData is declared without a library qualifier and thus defaults to Excel.ChartData 声明trpChartData没有库限定符,因此默认为Excel.ChartData

In addition to that you never cleared trpChartData with 除此之外,你永远不会清除trpChartData

Set trpChartData = Nothing

I also don't see where you .Quit the Excel application for the Chart.Workbook that must have been created. 我还没有看到你.Quit的Excel应用程序Chart.Workbook必须已经建立。 This could explain why there were versions of Excel open in the Task Manager afterwards. 这可以解释为什么之后在任务管理器中打开了Excel的版本。 Try adding this... 尝试添加此...

Dim xlApp as Excel.Application
'
'
Set xlApp = .Chart.ChartData.Workbook.Application
'
'
xlApp.Quit
Set xlApp = Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM