简体   繁体   中英

VBA: Positioning 3 Graphs On A Single Power Point Slide

I have VBA script that exports 3 graphs into the body of a single slide within an existing powerpoint template that I've created. My problem is that I'm not sure how to correctly position these graphs so that they are side by side by side. I've been trying to think of a way I can go about this and I've been trying to work out a For statement that increases the graph position with each graph.

My most recent attempt:

On Error Resume Next
    Set PowerPointApp = GetObject(class:="PowerPoint.Application")
    If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
    On Error GoTo 0
    Application.ScreenUpdating = False
    Set myPresentation = PowerPointApp.Presentations.Open(Filename:="X:\xxx\yyy.pptx")
    
'For i = 1 To Worksheets("Graphs").ChartObjects.Count
'Set cht = Worksheets("Graphs").ChartObjects(i)

MySlideArrayc = Array(12)
MyRangeArrayc = Array(Worksheets("Graphs").ChartObjects("A"), Worksheets("Graphs").ChartObjects("B"), Worksheets("Graphs").ChartObjects("CC"))

For xc = LBound(MySlideArrayc) To UBound(MySlideArrayc)
    'Copy Excel Range
        MyRangeArrayb(xc).CopyPicture

    Set shpc = myPresentation.Slides(MySlideArrayc(xc)).Shapes.Paste

Next

Next xc
    



myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Left = 50
myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Top = 50


myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Left = 150
myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Top = 150
    
Else
myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Left = 300
myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Top = 300

End If

End Sub

For one on top of the other, please try the next way:

myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Left = 50
myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Top = 50


myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Left = 50 
myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Top = 50 + _
     myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Height
    
myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Left = 50 
myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Top = 50 + _
    myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Height + _
    myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Height

For one near the other, please try the next way:

myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Left = 50
myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Top = 50

myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Left = 50 + _
    myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Width
myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Top = 50

myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Top = 50     
myPresentation.Slides(Worksheets("Graphs").ChartObjects("CC")).ShapeRange.Left = 50 + _
    myPresentation.Slides(Worksheets("Graphs").ChartObjects("A")).ShapeRange.Width + _
    myPresentation.Slides(Worksheets("Graphs").ChartObjects("B")).ShapeRange.Width

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM