使用vba将轴添加到powerpoint中的图表中?

时间:2012-01-31 07:15:05

标签: vba charts powerpoint powerpoint-vba

我正在研究一个项目。因为我想检查图表是否有x轴或y轴。如果没有,那么添加它。甚至我也想检查x或y轴是否有标题。如果没有,那么提供标题。

因为我写了一个代码,它检查x或y轴是否有标题。但如果没有,那么如何添加标题?

这是查找轴标题的代码

Dim oSld As Slide
Dim oShp As Shape
Dim oShapes As Shapes
Dim yaxes as Boolean
Dim xaxes as Boolean

 On Error GoTo Errorhandler:
For Each oSld In ActivePresentation.Slides

 Set oShapes = oSld.Shapes
 For Each oShp In oShapes
     If oShp.HasChart Then

                If oShp.HasChart Then

                    yaxes = oShp.Chart.Axes(xlValue, xlPrimary).HasTitle
                    xaxes = oShp.Chart.Axes(xlCategory).HasTitle

                    'check x axies have title
                    If xaxes <> True Then
                    ' Add title

                    End If 
                    'check y axies have title
                    If yaxes <> True Then
                    ' Add title

                    End If 
                End If
     End If
 Next oShp
Next

所以在上面的代码中,如果不分配,我也想添加轴。

感谢。

1 个答案:

答案 0 :(得分:1)

这样的事情

  1. 保持现有轴和/或标题不变
  2. 添加不存在的轴/标题

    Dim oSld As Slide
    Dim oShp As Shape
    Dim oShapes As Shapes
    For Each oSld In ActivePresentation.Slides
        Set oShapes = oSld.Shapes
        For Each oShp In oShapes
            If oShp.HasChart Then
                If oShp.HasChart Then
                    With oShp.Chart
                        If Not .HasAxis(xlValue) Then .HasAxis(xlValue) = True
                        If Not .HasAxis(xlCategory) Then .HasAxis(xlCategory) = True
                        If Not .Axes(xlCategory).HasTitle Then .Axes(xlCategory).HasTitle = True
                       If Len(.Axes(xlCategory).AxisTitle.Text) = 0 Then .Axes(xlCategory).AxisTitle.Text = "I'm X"
                        If Not .Axes(xlValue).HasTitle Then .Axes(xlValue).HasTitle = True
                       If Len(.Axes(xlValue).AxisTitle.Text) = 0 Then .Axes(xlValue).AxisTitle.Text = "I'm Y"
                    End With
                End If
            End If
        Next oShp
    Next oSld