在所选(或范围)Powerpoint幻灯片上循环显示图表

时间:2017-02-07 17:21:24

标签: vba powerpoint powerpoint-vba powerpoint-2013

我目前正在使用此代码更新我的powerpoint演示文稿中的所有链接:

Sub UpdateLinks()
Dim ExcelFile
Dim exl As Object
Set exl = CreateObject("Excel.Application")

ExcelFile = "C:\Users\J\Documents\Reporting\Governance Physical Charts.xlsm"

Dim i As Integer
Dim k As Integer

 'Go through every slide
For i = 1 To ActivePresentation.Slides.Count
    With ActivePresentation.Slides(i)
         'Go through every shape on every slide
        For k = 1 To .Shapes.Count
On Error Resume Next
             'Set the source to be the same as teh file chosen in the opening dialog box
            .Shapes(k).LinkFormat.SourceFullName = ExcelFile
            If .Shapes(k).LinkFormat.SourceFullName = ExcelFile Then
                 'If the change was successful then also set it to update automatically
                .Shapes(k).LinkFormat.AutoUpdate = ppUpdateOptionAutomatic 'other option is ppUpdateOptionManual
            End If

        Next k
    End With
Next i
End Sub

不是更新演示文稿中每个图表的链接,是否可以让这个代码循环只选择幻灯片?或者,如果它更容易 - 是否可以设置范围?例如,仅更新幻灯片15-30上的图表?

谢谢!

编辑: 评论中提供的解决方案 - 这是我修改后的代码

Sub UpdateLinks()
Dim ExcelFile
Dim exl As Object
Set exl = CreateObject("Excel.Application")
Dim sld As Slide

ExcelFile = "C:\Users\J\Documents\Reporting\Governance Physical Charts.xlsm"

Dim i As Integer
Dim shp As Shape

 For Each sld In ActivePresentation.Slides.Range(Array(11, 12, 13, 14, 15, 16, 17, 18))

        For Each shp In sld.Shapes
On Error Resume Next
            shp.LinkFormat.SourceFullName = ExcelFile
            If shp.LinkFormat.SourceFullName = ExcelFile Then
                shp.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic 'other option is ppUpdateOptionManual
            End If

        Next shp


Next
 End Sub

2 个答案:

答案 0 :(得分:1)

是的,您可以Slides以及Shapes上撰写自定义范围,使用Array作为index参数。试试这个:

Dim sld As Slide
For Each sld In ActivePresentation.Slides.Range(Array(1, 3, 5))
    Debug.Print sld.Name
Next

输出:

  

滑件   Slide4   Slide6

P.S。我在测试演示中删除了一张幻灯片。

答案 1 :(得分:0)

由于您还提到了处理刚选择的幻灯片,您可以这样做:

Sub SelectedSlides()
    Dim osl As Slide
    For Each osl In ActiveWindow.Selection.SlideRange
        Debug.Print osl.SlideIndex
    Next
End Sub

请注意,这将以REVERSE选择顺序为您提供所选幻灯片。也就是说,如果您按住Ctrl键单击幻灯片2,4,6,这将为您提供6,4,2。