PowerPoint VBA - Update linked graph from Excel at regular intervals

孤街醉人 提交于 2019-12-22 11:06:00

问题


I'm trying to build this system where I have this PowerPoint presentation with a linked graph from an external Excel file. I've seen that I can right click this graph in PowerPoint and click "Update link" and the graph is automatically updated.

But what if I want this automated? If this can be done without creating an Add-in that would have been great. So which event handlers are there in PowerPoint? I reckon there's an event for SlideChanged or something? Could I possibly have the presentation go in an endless loop and update the link at each new slide switch? There are possibly huge amounts of graphs. And one slide for each section of graphs.

Or, any other bright ideas? The system I'm trying to build is basically a framework for collecting data and displaying it in whatever form that might be wanted. Data is automatically imported from an economy software and into a database. So I've created a command line application that basically opens an Excel file and runs a macro (collecting the fresh data and copy it into the worksheet). This command line application is set to run as specific times via Scheduled Tasks. And it's this data that I want to show graphs from, automatically.

I actually did a large bit of it myself :)

Here's the code for VB.NET application (Can be used as command line application)

Imports Microsoft.Office.Interop
Public Class Form1

    Dim oPPTApp

    Sub updatePPTGraph()    
        For Each oSlide In oPPTApp.ActivePresentation.Slides
            For Each oShape In oSlide.shapes    
                If oShape.Type = 10 Then
                    oShape.LinkFormat.Update()
                End If

            Next
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oPPTApp = New PowerPoint.Application
        oPPTApp.visible = True
        Dim oPresentation As PowerPoint.Presentation

        oPresentation = oPPTApp.Presentations.Open("C:\Users\kenny\Documents\Charttest.pptx")
        updatePPTGraph()
    End Sub
End Class

This is clearly a start. I need to figure out if it can be done while a slideshow is showing as well. But I think it should be possible. Will update when I manage to get something worth mentioning :)


回答1:


Changing the underlying slides (ie from an external Excel app) should update the running show; the current slide in the running show won't usually show the update unless you force a redraw, but when it comes around in the show again, the update should be visible.

All told, it'd be easier to automate PPT from Excel than to let it control things and suck the updates in on demand ... at least it would unless you want to include an add-in to handle events in PPT.



来源:https://stackoverflow.com/questions/5964813/powerpoint-vba-update-linked-graph-from-excel-at-regular-intervals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!