更新PowerPoint图表而不打开图表工作簿或使其不可见

时间:2018-06-11 02:56:00

标签: vba powerpoint-vba

Sub OO()
Dim oPPApp As Object, oPPPrsn As Object, oPPSlide As Object
Dim oPPShape As Object
Dim FlName As String

'~~> Change this to the relevant file
FlName = "C:\Users\lich_\Documents\test.pptx"


'~~> Establish an PowerPoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")

If Err.Number <> 0 Then
    Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0

oPPApp.Visible = True
Set oPPPrsn = oPPApp.Presentations.Open(FlName, WithWindow:=msoFalse) 
Set oPPSlide = oPPPrsn.Slides(2)

With oPPSlide.Shapes("Chart1").Chart.ChartData
.ActivateChartDataWindow

.Workbook.Worksheets("Sheet1").Range("B2").Value = 0.1231
.Workbook.Close
End With



End Sub

如上所示,我正在尝试在vba中编辑图表数据。

但是由于我后来控制了很多图表,我想让工作簿不可见(或者如果可能的话根本不打开它)

With oPPSlide.Shapes("Chart1").Chart.ChartData
.ActivateChartDataWindow

.Workbook.Worksheets("Sheet1").Range("B2").Value = 0.1231
.Workbook.Close
End With

在这段代码中我用“ActivateChartDataWindow”方法打开并更改了我想要和关闭的数据。

有没有办法让窗口不可见或编辑数据而不打开?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您无需激活chartdata工作表即可对其进行更改。

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

您还可以将图表数据表设置为等于Excel工作表中的范围

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

答案 1 :(得分:0)

根据@ mooseman的答案,可以更新现有图表数据,而不是Activate

但是,如果图表在运行时是新的/插入的,据我所知,这不能通过互操作完成,因为AddChart方法添加了图表并同时创建/激活了Excel工作簿。虽然您可能不需要调用Activate方法,但无法插入或添加不涉及打开Excel实例的新图表。没有办法解决这个问题,这就是UI的功能和设计方式。

更新现有Chart / ChartData

中的数据

在本机PowerPoint VBA下面,但应该使用适当的引用轻松移植到Excel

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

要求是在VBA中使用With块。

一些简短的测试表明,使用win32com

从python进行Interop也是可行的
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

还有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;

        }
    }

最小化ChartData / Workbook窗口:

在实践中,我没有使用With方法获得可靠的运气。如果你不能让它工作,那么下一个最好的选择是立即最小化窗口:

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

请注意,此方法在屏幕上简要闪存Excel,并且此糟透了,因为在那个短暂的瞬间,它可以拦截击键/等。