在所有PPT图表上应用模板会导致“未定义用户定义的类型”错误

时间:2019-01-19 12:40:06

标签: vba powerpoint

我试图在我的PPT中的所有图表上应用模板,但出现错误提示

  

未定义用户定义的类型

我在网上找到了VBA,共享它的人说它对他有用。有什么建议么?我以为可能是路径中的破折号,但是使用“-”或“ _”无济于事。还尝试删除该路径后的最后一个括号。

Sub ChangeCharts()

    Dim myChart As ChartObject
    For Each myChart In ActiveSheet.ChartObjects
    myChart.Chart.ApplyChartTemplate ( _
    "Name\Users\Name\Library\Group Containers\UBF8T346G9.Office\User Content\Chart Templates\1.crtx")
    Next myChart

End Sub

尝试了新的VBA;

Sub ChangeCharts()
  Dim oSl As Slide
  Dim oSh As Shape

  For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
      Select Case oSh.Type
        Case Is = 3  ' Chart created in PPT
        Application.ActivePresentation.ApplyTemplate _
    "name/Users/name/Library/Group Containers/UBF8T346G9.Office/User Content/Chart Templates/1.crtx"

      End Select
    Next   ' oSh/Shape
  Next  ' oSl/Slide
End Sub

2 个答案:

答案 0 :(得分:0)

ActiveSheet是一个Excel对象。我认为您想在PowerPoint中使用ActiveSlide

答案 1 :(得分:0)

首先,请参见下面的注释,以了解为什么示例代码无法在PPT中工作:

Sub ChangeCharts()
    ' PPT has no ChartObject type  
    Dim myChart As ChartObject
    ' PPT has no ActiveSheet object
    For Each myChart In ActiveSheet.ChartObjects
    myChart.Chart.ApplyChartTemplate ( _
    "Name\Users\Name\Library\Group Containers\UBF8T346G9.Office\User Content\Chart Templates\1.crtx")
    Next myChart

End Sub

假设您是在PPT中运行此程序,则需要更多类似的内容:

Sub ChangeCharts
  Dim oSl as Slide
  Dim oSh as Shape

  For Each oSl in ActivePresentation.Slides
    For Each oSh in oSl.Shapes
      Select Case oSh.Type
        Case Is = 3  ' Chart created in PPT
          ' apply the template here
          With oSh.Chart
            .ApplyChartTemplate "drive:\path\template_name.crtx"
          End with  ' the chart
        ' Other case statements as needed to
        ' cover embedded/linked OLE objects 
        ' that are Excel charts
      End Select
    Next   ' oSh/Shape
  Next  ' oSl/Slide
End Sub