特定幻灯片编号的PowerPoint循环

时间:2015-10-30 10:52:29

标签: vba ms-office powerpoint powerpoint-vba

我有多个带有Excel图表的幻灯片组,需要格式化以便在PPT中更好地显示(增加字体大小)。

我想出了下面的代码,但无法插入循环,因此此代码仅适用于幻灯片36到45。

我已经尝试过以下代码,但不起作用,我认为这是因为它引用了ActivePresentation,而我的格式代码引用了ActiveWindow,但我没有使用PPT VBA知识解决这个问题。

Dim oPresentation As Presentation
Set oPresentation = ActivePresentation

Dim oSlide As Slide
Dim oSlides As SlideRange
Dim oShape As Shape
Dim slideNumber As Integer

For slideNumber = 36 To 45

 >>>REST OF MY CODE INSERTED HERE<<<

Next slideNumber
Sub FormatChartPPT()

'PURPOSE: Increase font size of chart data labels, legend, axes values and categories

Dim ocht As Chart
Dim i As Integer
Dim shp As Shape


'Auto select chart
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasChart Then .Select
   End With
  Next shp


'Apply to selected chart
Set ocht = ActiveWindow.Selection.ShapeRange(1).Chart


'Format data labels
For i = 1 To ocht.SeriesCollection.Count

If ocht.SeriesCollection(i).HasDataLabels Then

With ocht.SeriesCollection(i).DataLabels.Font
.Size = 14
End With

End If

Next i


'Format legend
If ocht.HasLegend Then
With ocht.Legend.Font
.Size = 14
End With
End If

'Format axis values
With ocht.Axes(xlValue).TickLabels.Font
.Size = 14
End With

'Format axis category
With ocht.Axes(xlCategory).TickLabels.Font
.Size = 14
End With


End Sub

1 个答案:

答案 0 :(得分:1)

您应该创建一个新方法,该方法可以根据需要绘制幻灯片并在其上格式化形状。假设我们将其定义为Sub FormatShapes(sld As Slide)(请参阅后面的代码)。然后应在主循环中调用此方法,例如:

For SlideNumber = 36 To 45
    FormatShapes ActivePresentation.Slides(SlideNumber)
Next SlideNumber

FormatShapes可能如下所示:

Sub FormatShapes(sld As Slide)
    Dim ocht As Chart
    Dim shp As Shape

    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set ocht = shp.Chart
            ... Do shape chart formatting using ocht ...
        End If
    Next shp
End Sub