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

Apply template on all PPT charts causes "User-defined type not defined" error

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

User-defined type not defined

我在网上找到了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.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

ActiveSheet 是一个 Excel 对象。我想你想为 PowerPoint 使用 ActiveSlide

首先,请参阅下面的评论以了解为什么您的示例代码无法在 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.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