使用ActivateChartDataWindow

时间:2017-02-07 00:08:00

标签: vba powerpoint

我在powerpoint中构建图表自动化脚本,在调用" ActivateChartDataWindow"时,我有任何问题。

我会使用"激活"而不是" ActivateChartDataWindow",但是"激活"加载完整的Excel程序,使整个例程运行缓慢和过时。

我遇到的问题是" ActivateChartDataWindow"确实可以填充图表,但是当我手动编辑数据时 - 右键单击​​,编辑数据 - 访问excel应用程序,它似乎不想加载!

在过去的5个小时里,它一直让我发疯,并且不胜感激任何关于如何过来的想法。

OLE.dlll正常工作,我正在使用的代码如下。

以下代码:

每页有5个幻灯片,每个页面都有一个图表,下面的代码就是我用作概念点的内容

我正在使用" ActivateChartDataWindow"错了,但网上没有太多知道我做错了什么! Arrrhhhh!

    For i = 1 To 5
        Set instance = Nothing
        Set instance = ActivePresentation.Slides(i).Shapes(1).Chart.ChartData
        With instance
            .ActivateChartDataWindow
            instance.Workbook.Sheets(1).Range("A1:H26").Value = 27
            instance.Workbook.Close
        End With
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

如同一直推荐的那样,您不需要Activate一个对象来修改它。如果您正在尝试处理幻灯片中嵌入的工作簿,则可以这样做

' This function will get you a Workbook object embedded in a Slide (late binding)
Function getEmbeddedWorkbook(sld As Slide) As Object
    Dim shp As Shape
    On Error Resume Next
    For Each shp In sld.Shapes
        If shp.Type = 3 Then ' embedded chart workbook created in PP
           Set getEmbeddedWorkbook = shp.Chart.ChartData.Workbook
           Exit Function
        End If
        If shp.Type = 7 Then ' embedded workbook pasted from excel
            Set getEmbeddedWorkbook = shp.OLEFormat.Object
            Exit Function
        End If
   Next
End Function


' For Testing, I have 6 slides, Some have a workbook pasted from Excel
' OLE, shape type = 7, others have a chart created in PP (type = 3)
Sub Test()
    Dim wb As Object, i As Long
    For i = 6 To 6 'ActivePresentation.Slides.Count
        Set wb = getEmbeddedWorkbook(ActivePresentation.Slides(i))
        If Not wb Is Nothing Then
            wb.Sheets(1).Range("A1:D5").Value = i * i
        End If
    Next
End Sub