在所有PPT图表上应用模板导致错误"User-defined type not defined"



我正在尝试在我的 PPT 中的所有图表上应用模板,但收到一条错误消息

未定义用户定义类型

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

Sub ChangeCharts()
    Dim myChart As ChartObject
    For Each myChart In ActiveSheet.ChartObjects
    myChart.Chart.ApplyChartTemplate ( _
    "NameUsersNameLibraryGroup ContainersUBF8T346G9.OfficeUser ContentChart Templates1.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

首先,请参阅下面的评论以了解为什么您的示例代码无法在 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 ( _
    "NameUsersNameLibraryGroup ContainersUBF8T346G9.OfficeUser ContentChart Templates1.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:pathtemplate_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

ActiveSheet是一个Excel对象。我想你想把ActiveSlide用于PowerPoint。

相关内容

最新更新