1
votes

The strangest thing is happening with my macro in Excel. It works like a charm, but when it has to copy 2 charts and paste into my powerpoint presentation, suddenly, the Chart isn't exactly the same.

My code:

Set Wb = Workbooks.Open("Path\WbName.xlsx", ReadOnly:=True, UpdateLinks:=0) 

It opens 5 more workbooks... And then it goes through a loop, to copy all the Charts

Dim Charts_Arr As Variant
    Charts_Arr = Worksheets("Parameters").ListObjects("Parameters").DataBodyRange.Value

For i = LBound(Charts_Arr) To UBound(Charts_Arr)
    SourcePath = Charts_Arr(i, 8)
    SheetName = Charts_Arr(i, 4)
    ShapeNr = Charts_Arr(i, 2)
    SlideNr = Charts_Arr(i, 3)
    Schaling = Charts_Arr(i, 6)

    Set Source = Workbooks(SourcePath)
    Set PPpres = oPPTApp.ActivePresentation
    Set Sh = Source.Sheets(SheetName).Shapes(ShapeNr)


    Sh.Copy


    Set NewSh = PPpres.Slides(SlideNr).Shapes.PasteSpecial(ppPasteJPG)
    With NewSh
      .Top = Charts_Arr(i, 5)
      .Left = Charts_Arr(i, 7)
      .ScaleHeight Schaling, msoTrue
    End With

Next i

This goes perfectly. But when I take a look at the ppt-file, 2 charts are not exactly the same.

(TIP: Excel is Chartarea, not a shape - didn't know this at first)

The white line drops after 27/11

When copy the picture manually, I get the correct picture:

enter image description here

And what's more bizarre, I have 2 other Charts on another Sheet in the same workbook who doesn't cause any problems.

Could this be a problem with links, or the way I copy?

UPDATE

If I adjust the code as suggested below:

 Source.Sheets(SheetName).ChartObjects(ShapeNr).Chart.CopyPicture
        Set NewSh = PPpres.Slides(SlideNr).Shapes.Paste
            With NewSh
                        .Top = Charts_Arr(i, 5)
                        .Left = Charts_Arr(i, 7)
                        .ScaleHeight Schaling, msoTrue
            End With  

I get this:

CopyPicture

I'm doing something wrong with the Paste part of the code, I guess. Tried other possibilities, always end up getting no images, or the one above.

FIXERSUPDATE

So I made/used a loophole. Couldn't find a way to paste the images directly into Powerpoint, So I pasted it into an excelsheet 'Temp' instead. And adjusted the Array, and that seemed to work. But I still would like to know how to do this directly in Powerpoint.

Thanks in advance for your insights!

1
Do you have a chart which matches the pasted one? - Tim Williams
Did you try to copy the chart with copypicture method? Wb.Sheet1.ChartObjects(1).Chart.CopyPicture where 1 is the index of the chart. And instead of jpg, other paste special data types? - danieltakeshi
@TimWilliams Picture Nr 1 is the outcome after the macro, Picture Nr 2 is the original Chart. - Nathalii.
@danieltakeshi I made an update, tnx for the tip! - Nathalii.
@Nathalii. Unfortunately I don't use VBA for PowerPoint, however, even when I try to copy/paste manually some charts, It seems to get out of formatting. Maybe some issue between both programs. Let's try pasting another datatype, like Set NewSh = PPpres.Slides(SlideNr).Shapes.Paste ppPasteEnhancedMetafile or Set NewSh = PPpres.Slides(SlideNr).Shapes.Paste ppPasteMetafilePicture. Refer to this for other data types. - danieltakeshi

1 Answers

0
votes

I couldn't find a PasteSpecial Option that could fix the issue. CopyasPicture works, but I can't seem to figure out how to paste it directly into ppt. So I used a workaround. I created a 'Temp' Sheet, where I could paste the Chart as Picture in the right format, afterwards I could program it to paste the shape into Ppt. Not the cleanest way to solve the issue, but it works.