1

I need to copy some charts from excel to a ppt presentation. The presentation stays the same with some placeholders to to receive the charts that get updated every now and then. I need to be able to decide what charts goes to what placeholder because I want to be able to change the excel file or the ppt without constraints. Therefore I want to use a .json file with an external dictionary which tells me which chart from which sheet goes to which slide in which placeholder. It looks like this

{'chart1':{'xl_sheet_name':'Summary numbers',
       'xl_chart_name':'Chart 9',
       'ppt_tgt_slide':10,
       'ppt_tgt_placeholder':2},
 'chart2':{'xl_sheet_name':'Summary numbers',
       'xl_chart_name':'Chart 10',
       'ppt_tgt_slide':10,
       'ppt_tgt_placeholder':3},
 'chart3':{'xl_sheet_name':'Summary numbers',
       'xl_chart_name':'Chart 11',
       'ppt_tgt_slide':10,
       'ppt_tgt_placeholder':4}}

Then the code goes

Sub charts_2_ppt()
Dim strText As String
Dim lib As New JSONLib
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide

'Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
'Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide

strText = GetFileContent(ThisWorkbook.Path & "\parameters.json")

Set charts_dict = lib.parse(CStr(strText))

'routine
For Each v In charts_dict
    Worksheets(charts_dict(v)("xl_sheet_name")).ChartObjects(charts_dict(v)("xl_chart_name")).Copy 'ChartArea.Copy
    With PPPres
        .Slides(charts_dict(v)("ppt_tgt_slide")).Select 'otherwise it's messed up.
        .Slides(charts_dict(v)("ppt_tgt_slide")) _
            .Shapes.Placeholders(charts_dict(v)("ppt_tgt_placeholder")).Select 'msoCTrue
        .Windows(1).View.PasteSpecial '(ppPasteMetafilePicture)
    End With

    'Sheets(charts_dict(v)("xl_sheet_name")) _
        .ChartObjects(charts_dict(v)("xl_chart_name")).Chart.HasTitle = True

Next

Set charts_dict = Nothing
Set lib = Nothing
End Sub

Function GetFileContent(Name As String) As String
Dim intUnit As Integer

On Error GoTo ErrGetFileContent
intUnit = FreeFile
Open Name For Input As intUnit
GetFileContent = Input(LOF(intUnit), intUnit)
ErrGetFileContent:
Close intUnit
Exit Function
End Function

probably adapted from john peltier's site and some other stuff I found online. The sense of the dictionary is that in a slide I have 4 placeholders, the first is the title, than there are three charts to paste. I made a loop because the charts are going to be many, to different slides. I took it out of VBA because it looks nicer and to be able tochange it quickly.

My problem is that when I run the code, the third placeholder is not apparently selected and the chart is pasted in the middle of the slide, instead of into the place where it belongs.

Can you please help me make it work and/or suggest other suitable ways?

Thanks a lot,

Filippo

ps The class I used for parsing javascript, I originally found it online, but the link now is down. Anyway I changed something so here it is: my java parser.

4

0 回答 0