将在Power Point演示中找到命名图表的VBA代码

时间:2017-05-19 05:04:01

标签: vba loops charts powerpoint

在网站成员的支持下,我能够在Power Point幻灯片中创建用户表单以更新嵌入式图表。为了充分利用用户表单,VB代码必须用于遍历所有幻灯片,因为命名图表可能在幻灯片之间移动。图表的名称是“DVPVchart”。正在使用的代码在第一个Set赋值时继续出错。代码如下。尝试了循环的多个排列和变体,但继续返回相同的错误。任何指导都表示赞赏。

Private Sub AddDVSetUp_Click()

Dim sld As slide
Dim shp As shape
Dim chrt As Chart
Dim xlWB As Object


    For Each sld In ActivePresentation.Slides

        Set shp = sld.Shapes("DVPVchart")
        Set xlWB = shp.Chart.ChartData.Workbook

        'Find first sheet of embedded Chart In PowerPoint 
        With xlWB.Sheets(1)

            'location in Chart In PowerPoint = UserForm Textbox 
            .Range("C4").Value = Gate2Date.Value
            .Range("C11").Value = OldestSurrogateDate.Value

        End With

    Next sld

End Sub

1 个答案:

答案 0 :(得分:1)

它返回错误,因为您没有检查图表是否存在于所述幻灯片中。如果图表符合您的条件,您需要添加一个检查。尝试:

For Each sld In ActivePresentation.Slides
    '/* You will need another loop to check each shape */
    For Each shp In sld.Shapes
        If shp.Type = msoChart Then '/* check for specific type of shape */
            If shp.Name = "DVPVchart" Then '/* chech chart for specific name */
                Set xlWB = shp.Chart.ChartData.Workbook '/* assign it */
                Exit For '/* exit since you got what you need */
            End If
        End If
    Next
    If Not xlWB Is Nothing Then Exit For '/* exit if you already set your xlWB object
Next

'/* Rest of your code go here */