2017-11-22 9 views
1

私はPowerpointのチャートを扱うためのコードを見つけ、修正し、テストしました。PPTで使用される2つのVBAコードを組み合わせる

最初の部分は、(スライド上に2つのプロットがあると仮定した場合)のプロットと同じ大きさにし、それらを正しく位置付けであり、このように書きます:

Sub ProcessAllSlides() 

Dim sld As Slide 
Dim Shp As Shape 

For Each sld In ActivePresentation.Slides 
    Call SetChartSizeAndPosition(sld.Shapes(1), 30, 120, 320, 240) 
    Call SetChartSizeAndPosition(sld.Shapes(2), 370, 120, 320, 240) 
Next 

End Sub 

Sub SetChartSizeAndPosition(Shp As Shape, Left As Single, Top As Single, Width As Single, Height As Single) 

With Shp 
    .Left = Left 
    .Top = Top 
    .Width = Width 
    .Height = Height 
End With 

End Sub 

これは、プレゼンテーションのすべてのスライドに取り組んでいます。私はそれがアクティブなスライド上でのみ機能するようにしたいと思います。

第2の部分は、プロット領域(チャート領域ではない)の書式を設定することになっています。以下を参照してください:

Sub SizePlotArea() 

Dim oSld As Slide 
Dim oCht As Chart 

Set oCht = ActiveWindow.Selection.ShapeRange(1).Chart 

With oCht.PlotArea 
    ' Edit these values as needed 
    ' Change the following lines to e.g. Msgbox .Left etc 
    ' to get the values of the chart you want to match others TO 
    .Left = 0 
    .Top = 0 
    .Height = 220 
    .Width = 300 
End With 

End Sub 

Idealy私はアクティブなスライド上の2つの(2)それらはすべてのために行われていることを、このようなグラフを組み合わせるしたいと思います。

どのようなヒントがありますか?

答えて

1

以下を試してください。Subをコードのコメントの中に入れてください。

Option Explicit 

Sub ProcessAllSlides() 

Dim sld As Slide 
Dim Shp As Shape 
Dim oCht As Chart 
Dim i As Long 
Dim ChartIndex As Long 

' set the Active Slide 
Set sld = Application.ActiveWindow.View.Slide 

ChartIndex = 1 

' --- loop through the Slide shapes and search for the Shape of type chart 
For i = 1 To sld.Shapes.Count 
    If sld.Shapes(i).HasChart = msoTrue Then ' if current shape is a chart 
     Set Shp = sld.Shapes(i) 
     Set oCht = Shp.Chart 

     If ChartIndex = 1 Then ' first chart 
      SetChartSizeAndPosition Shp, 30, 120, 320, 240 
      ChartIndex = ChartIndex + 1 

     ElseIf ChartIndex = 2 Then ' second chart 
      SetChartSizeAndPosition Shp, 370, 120, 320, 240 
     End If 

     With oCht.PlotArea 
      ' Edit these values as needed 
      ' Change the following lines to e.g. Msgbox .Left etc 
      ' to get the values of the chart you want to match others TO 
      .Left = 0 
      .Top = 0 
      .Height = 220 
      .Width = 300 
     End With 

     Set oCht = Nothing 
     Set Shp = Nothing 
    End If 
Next i 

End Sub 
+0

それを感謝します。それが私が探していたものでした。何らかの理由で、他のコードの一部が別のモジュールにある場合に実行されます。私が自分でコードを実行しただけでは、 "SetChartSizeAndPosition Shp、30、120、320、240"行に "コンパイルエラー"(Subまたは関数が定義されていない)が表示されます。それがどんなアイデアなのか? – pApaAPPApapapa

+0

それを考え出した。もちろん、上のdefineサブが必要です。申し訳ありませんが、偉大な答えに感謝 – pApaAPPApapapa

+0

@pApaAPPApapapaあなたは歓迎です:) –

関連する問題