Update PowerPoint chart without opening chart workbook or making it invisible

断了今生、忘了曾经 提交于 2019-11-28 01:44:14

It's possible to update existing chart data without Activate as per @mooseman's answer.

However, if the chart is new/inserted at runtime, as far as I know this cannot be accomplished with interop, as the AddChart method adds the chart and simultaneously creates/activates the Excel Workbook. While you may not need to call the Activate method, there is no way to insert or add a new chart that doesn't involve opening an Excel instance. There is no way around this, this is just how the UI functions and it is by design.

To Update Data in EXISTING Chart / ChartData

Below native PowerPoint VBA, but should port easily to Excel with proper reference(s)

Sub test()
    Dim PPT As PowerPoint.Application
    Dim pres As Presentation
    Dim sld As Slide
    Dim shp As Shape
    Dim cht As Chart
    Dim rng As Object ' Excel.Range

    Set PPT = Application 'GetObject(,"PowerPoint.Application")
    Set pres = ActivePresentation
    Set sld = pres.Slides(1)
    Set shp = sld.Shapes(1)
    Set cht = shp.Chart

    Call changeData(cht, 6.3)

    pres.Slides.AddSlide pres.Slides.Count + 1, sld.CustomLayout
    Set sld = pres.Slides(pres.Slides.Count)
    sld.Shapes.AddChart().Chart.ChartData.Workbook.Application.WindowState = -4140

    Set cht = sld.Shapes(1).Chart
    Call changeData(cht, 3.9)

End Sub

Sub changeData(c As Chart, v As Double)
    Dim rng As Object
    With c.ChartData
        Set rng = .Workbook.Worksheets(1).ListObjects(1).Range
        rng.Cells(2, 2).Value = v ' etc.
        .Workbook.Close
    End With
End Sub

The requirement is to use the With block in VBA.

Some brief tests suggest this is also doable via Interop from python using win32com:

from win32com import client
ppt = client.Dispatch("PowerPoint.Application")
pres = ppt.ActivePresentation
sld = pres.Slides[0]
cht = sld.Shapes[0].Chart
cht.ChartData.Workbook.Worksheets[0].ListObjects[0].Range.Cells(2,2).Value = 9

And also in C#:

    using Microsoft.Office.Interop.PowerPoint;

    public static void foo(int value = 10)
    {

        Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
        Presentation pres = ppt.ActivePresentation;
        Slide sld = pres.Slides[1];
        Chart cht = sld.Shapes[1].Chart;
        {
            cht.ChartData
                .Workbook.Worksheets[1].ListObjects[1].Range.Cells(2, 2).Value = value;

        }
    }

To Minimize the ChartData / Workbook Window:

In practice I have not had reliable luck using the With method. If you cannot get it to work, then the next-best option is to minimize the window immediately:

Sub changeData(c As Chart, v As Double)
    Dim rng As Object
    With c.ChartData
        .Activate
        .Workbook.Application.WindowState = -4140 '## Minimize Excel
        '## DO STUFF:
        Set rng = .Workbook.Worksheets(1).ListObjects(1).Range
        rng.Cells(2, 2).Value = v ' etc.
        .Workbook.Close
    End With
End Sub

Note that this method does briefly flash Excel on the screen, and this sucks because in that brief instant, it can intercept keystrokes/etc.

You don't have to activate the chartdata worksheet to make changes to it.

 With oPPSlide.Shapes("Chart1").Chart.ChartData
 'this updates the value in the datasheet
    .Workbook.Sheets(1).Range("B2").Value = 0.1231 
End with

You can also set the chartdata sheet to equal a range in an excel sheet

path2 = "C:\JohnDoe\Vasquez_061118.xlsm"
Set xlWorkBook = Workbooks.Open(FileName:=path2, ReadOnly:=True)
     With oPPSlide.Shapes("Chart1").Chart.ChartData
     'this updates the value in the datasheet
.Workbook.Sheets(1).Range("A1:B2").Value = xlWorkBook.Sheets(1).Range("A2:B3").Value
   End With
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!