vba - 更改 PowerPoint 直方图中图表标题的文本颜色

标签 vba vsto office-interop powerpoint

我正在尝试更改 PowerPoint 中直方图图表标题的文本颜色。
这是我所做的:

var colorFormat = chart.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor;
colorFormat.RGB = ...;
// or
colorFormat.ObjectThemeColor = ...;

这适用于折线图等标准图表。但它不适用于其他图表类型,如直方图、瀑布图、树形图等。

在这些情况下,设置ObjectThemeColor会将文本设置为黑色。设置 RGB 实际上设置了正确的颜色。但是,在这两种情况下,一旦用户更改选择,文本颜色就会跳回之前的颜色。

如何设置这些图表之一的标题文本颜色?
我使用的是 VSTO 和 C#,但 VBA 解决方案同样受欢迎,只要它可以转换为 C# 并且仍然有效。

最佳答案

根据您提供的信息,我在 PowerPoint 中构建了直方图和瀑布图,并成功使用:

Sub ChartTitleFontColor()
  Dim oShp As Shape
  Dim oCht As Chart

  'Waterfall on slide 1
  Set oShp = ActivePresentation.Slides(1).Shapes(1)
  If oShp.HasChart Then
    Set oCht = oShp.Chart
  End If

  ' Do stuff with your chart
  If oCht.HasTitle Then
    Debug.Print oCht.ChartTitle.Text
    oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
  End If

  'Histogram on slide 2
  Set oShp = ActivePresentation.Slides(2).Shapes(1)
  If oShp.HasChart Then
    Set oCht = oShp.Chart
  End If

  ' Do stuff with your chart
  If oCht.HasTitle Then Debug.Print oCht.ChartTitle.Text
    oCht.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(251, 5, 40)
  End If

  ' Clean up
  Set oShp = Nothing
  Set oCht = Nothing
End Sub

enter image description here

关于vba - 更改 PowerPoint 直方图中图表标题的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465151/

相关文章:

.net - Excel 互操作移动/设置图像位置

c# - 使用 Microsoft.Office.Interop 向 MS Word 表格添加一行

c# - 处置Microsoft.Office.Interop.Word.Application

vba - Excel VBA - 将正确的参数传递给过程

vba - 基本语法和示例教程

Outlook 功能区 XML - 当其他电子邮件项目打开时获取事件邮件项目

c# - 将 HRESULT 转换为可读消息

excel - 从输入框中跳过同名目录中的文件

excel - 数据透视图格式

c# - 单元测试 VSTO 项目