0
votes

I am trying to copy some charts (embedded) in different worksheets of an excel workbook using vbscript into different slides of a powerpoint. I would like to keep the link between the excel sheet and the powerpoint while doing so and therefore I used the below piece of code that allows me to keep formatting and link (instead of a simple paste or PasteSpeical. Is there any other way?) :

For i = 1 to TotalNumWorkSheets 'I iterated with indices.
    Set pptSlide = pptPres.Slides.Add(i, 11) 'There is one opening slide before this.
    set ws = wb.Worksheets(i)
    ws.ChartObjects(1).Chart.ChartArea.Copy
    pptApp.CommandBars.ExecuteMso("PasteExcelChartSourceFormatting")
    pptApp.CommandBars.ReleaseFocus

    With pptSlide
        .Shapes.Title.TextFrame.TextRange.Text = objCurSheet.Name
        'Adding some more textboxes here. Working fine. Position checks out in PPT.
        .Shapes(.Shapes.Count).Left = 20  'Doesn't work for all slides.
    End With
Next

The code works and copies all the charts, creates titles, adds new text as expected, but I am not able to position the charts on individual slides because after the ExecuteMso command, I don't know how to access the reference to the chart. I read in one of the SO posts that pasting using this method looses the chart selection but you can access the last .Shapes object since pasting always adds the object to the end of the list. Is that always the case? I tried accessing and positioning my chart by accessing the last object but it only works for the first slide of the loop (i.e. the first chart pasted is shifted to Left=20). The rest all charts in other slides are centered. Can someone explain where and how to add the formatting chart options? I ran into an even weirder problem. If I increase the number of worksheets, even the first plot looses it's Left formatting. The above code is the only place where I add formatting so I don't know what is happening. I am sure I am not formatting it correctly.

Many thanks for your suggestions.

Edit: One additional thing which I tested. I am using WScript.Sleep 500 code before the For loop ends since it gives enough time for earlier operations to finish (at least that's what I understood from many other google searches).

1
Ignore last comment.... WScript.Sleep is VBScript :)Darren Bartrup-Cook

1 Answers

0
votes

So after some more searching and testing, I found a solution (for my case at least).

  1. Moved the WScript.Sleep 1000 (500 didn't work for me) statement just below CommandBars.ReleaseFocus.

It makes some sense now since it is the Chart copying, pasting and linking from excel that needs time. Especially with source formatting. After that there is only text generation which I believe is not so heavy.

  1. Cleared all the set Object variables when not used. Especially the ones associated with the "With" keyword.
set obj = CreateObject() 
With obj
    'Do something here.
End With
set obj = Nothing

Not clearing them, apparently, can also prevent you from closing the applications even after you use the .Close and .Quit method. At least that's what I observed. I found PowerPoint.exe running in the task manager when nothing was opened.

After doing the above, I am able to copy with format the charts and also able to set the position of the charts. The weird problem of larger number of worksheets also disappeared. Hopefully, it might help others. If someone thinks the observations are incorrect or troublesome, please correct.