简体   繁体   中英

Paste Chart and Change Size in Power Point - VBA

I'm trying paste a chart in PowerPoint using the command:

PPApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"

It's working very well, but when I try to change size and position the code don't make effect in chart properties and don't return errors. When I use the command:

Shapes.Paste.Select

I can change and resize the chart, but don't work for me because I want "PasteExcelChartSourceFormatting". What are my mistakes?

My full code With PPSlide

    ActiveChart.ChartArea.Copy

    PPApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"

    '.Shapes.Paste.Select

    With PPApp.ActiveWindow.Selection.ShapeRange
        .LockAspectRatio = msoFalse
        .Width = 680.314961
        .Height = 453.543307
        .Left = 19.8425197
        .Top = 56.6929134
    End With

Try something along these lines:

Dim oSh As Object ' or PowerPoint.Shape if you've set a reference to PPT
Dim lSlideIndex As Long

lSlideIndex = 1

CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"
Set oSh = ActivePresentation.Slides(lSlideIndex).Shapes(ActivePresentation.Slides(lSlideIndex).Shapes.Count)

With oSh
    .Left = 0
    .Width = 100
End With

I tried to do this, but doesn't work. Seems that the presentation doesn't update the shapes numbers. I tried to this:

lSlideIndex = oPPtApp.ActiveWindow.View.Slide.SlideIndex

MsgBox oPPtApp.ActivePresentation.Slides(lSlideIndex).Shapes.Count

oPPtApp.CommandBars.ExecuteMso "PasteExcelChartSourceFormatting"

MsgBox oPPtApp.ActivePresentation.Slides(lSlideIndex).Shapes.Count

The command is executed and the shape entered, but the msgbox returns the same value before and after the command. "oPPtApp.CommandBars.ExecuteMso" But when I run in different routines, the numbers have been updated. As if I run a routine to the command "oPPtApp.CommandBars.ExecuteMso" and one for resizing shapes works perfectly

Seems to be a common problem when calling ExecuteMso in PowerPoint. Try inserting:

Application.DoEvents()

right after the ExecuteMso() command. That works in most (but not all, in my experience) situations.

If MSFT would simply expose these paste methods and other thing (like Undo) in the object model, that would avoid the need to call ExecuteMso and make like easier for developers.

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