PDA

View Full Version : VBA - Way to recreate chart from excel in powerpoint (not paste or embed)



rheseus
05-17-2024, 05:14 AM
Basically, I should be able select the chart in excel, run the macro, the macro will open a new instance of PowerPoint, insert a new chart (with the same chart type as the one in excel) and update the ppt chart backend data with the data from the excel chart
Hope this was clear. Any support would be appreciated!
All content I found online only suggests code for copy-pasting with (a) link to excel or (b) an embedded chart
(a) doesn't work because user should be able to edit chart
(b) doesn't work because user should not have access to full excel file and will also increase size of ppt
Apologies if I have missed an existing solution here.
Adding my sample code that can paste as well as embed...


Sub ExportSelectedChart()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim Chrt As ChartObject
'Set Chrt = ThisWorkbook.Sheets("Sheet1").ChartObjects(1)
If TypeName(Selection) = "ChartArea" Then
Set pptApp = New PowerPoint.Application
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Add
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
Set Chrt = Selection.Parent.Parent
Chrt.Copy
'Pastes Chart - But linked to source excel
'PPTSlide.Shapes.Paste
'Pastes Embedded Chart - But includes full excel file and size
pptApp.CommandBars.ExecuteMso ("PasteExcelChartSourceFormatting")
Else
MsgBox "Please select a chart in Excel before running this macro.", vbExclamation
End If
End Sub

Aussiebear
05-17-2024, 10:14 AM
Welcome to VBAX rheseus. Have you looked at the following concept? https://support.microsoft.com/en-au/office/insert-and-update-excel-data-in-powerpoint-0690708a-5ce6-41b4-923f-11d57554138d#:~:text=In%20PowerPoint%2C%20on%20the%20Insert,select%20Link%2 C%20and%20click%20OK.

rheseus
05-17-2024, 09:46 PM
Thanks for sharing the link. It still doesn't solve my problem though, the options listed are pasting with link, without or embedding, neither of which work for my situation.

My solution would be closest to the embed option but only the chart data is embedded, not the rest of the excel

Aussiebear
05-17-2024, 11:45 PM
Then try this link. https://answers.microsoft.com/en-us/msoffice/forum/all/auto-updating-linked-data-between-excel-and/b5ba3b06-778d-488a-99d6-55e2384e5569