我在 PowerPoint 中使用 VBA 时遇到运行时错误 438。具体的错误消息是“未找到方法或数据成员”,它发生在我的代码中的
Set chartTitleTextFrame = cChart.ChartTitle.TextFrame
行。这是相关片段:
' Check if the chart has a title
If cChart.HasTitle Then
Set chartTitleTextFrame = cChart.ChartTitle.TextFrame
' Set chart title text and font properties
With chartTitleTextFrame.TextRange.Font
.Name = "Montserrat"
.Bold = True
.Size = 26
.Color.RGB = RGB(107, 27, 85)
End With
我已经仔细检查了对象层次结构,根据文档,似乎
cChart.ChartTitle
确实具有 TextFrame
属性。但是,当我运行代码时,它会抛出 438 错误。如果我删除 .TextFrame
,则会收到 13 错误。我不确定为什么我无法在此特定上下文中访问 TextFrame
属性。
任何人都可以深入了解可能导致此错误的原因以及我如何解决它吗?非常感谢任何帮助!
附加信息:
Dim chartTitleTextFrame As TextFrame
) 并将其设置为 cChart.ChartTitle.TextFrame
。.TextFrame2
代替.TextFrame
,但遇到了同样的问题。如果需要任何其他信息,请告诉我。预先感谢您的帮助!
提供了循环浏览第一张幻灯片上所有图表的代码。根据需要更新。
使用
.ChartTitle.Format.TextFrame2.TextRange.Font
更改字体属性,使用.Fill.ForeColor.RGB
更改字体颜色。
Sub SetChartTitleFont()
Dim oSlide As Slide
Dim oShape As Shape
Dim oChart As Chart
Set oSlide = ActivePresentation.Slides(1)
For Each oShape In oSlide.Shapes
If oShape.HasChart Then
Set oChart = oShape.Chart
With oChart.ChartTitle.Format.TextFrame2.TextRange.Font
.Name = "Montserrat"
.Bold = True
.Size = 26
.Fill.ForeColor.RGB = RGB(107, 27, 85)
End With
End If
Next oShape
End Sub