Copying and pasting charts created in Excel into PowerPoint slides manually can be a hassle.
In this article, I will share the code and steps to automate pasting graphs from Excel to PowerPoint using VBA.
目次
Objectives
- Paste an existing chart from Excel into PowerPoint using VBA.
- Load a PowerPoint template file and paste the chart onto the first slide.
- (Future Goal) Control the size and position of the pasted chart.
VBA Code Used
Below is the basic code to paste a chart from Excel VBA to PowerPoint.
Sub ExportGraphToPowerPoint()
Dim ppApp As Object
Dim ppPres As Object
Dim ppSlide As Object
Dim ChartObj As ChartObject
' PowerPointアプリケーションを起動
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True
' PowerPointのテンプレートを開く
Set ppPres = ppApp.Presentations.Open("テンプレファイルがあるPATH") ' 例: "C:\Users\...\テンプレ.pptx"
' Excel側のグラフをコピー
Set ChartObj = ThisWorkbook.Sheets("シート名").ChartObjects("グラフ名")
ChartObj.Copy
' PowerPointの1枚目のスライドに貼り付け
Set ppSlide = ppPres.Slides(1)
ppSlide.Shapes.Paste
' PowerPointファイルを保存
ppPres.Save
' 使用したオブジェクトを解放
Set ppSlide = Nothing
Set ppPres = Nothing
ppApp.Quit
Set ppApp = Nothing
End Sub
Key Points
- “テンプレファイルがあるPATH”: Enter the actual file path here.
- Example:
"C:\Users\user\Documents\Template.pptx"
- Example:
- “シート名” / “グラフ名”: “シート名” is the Excel sheet name containing the graph, and “グラフ名” is the name of the chart object.
- Example:
Sheets("Analysis").ChartObjects("SalesChart")
- Example:
Ideas for Future Expansion
This code only covers the “pasting” process. Adding the following features will make it more practical:
- Specify size and position: Example: Set 100pt from the left and 10cm wide.
- Paste as “Picture”: Using
PasteSpecialto make the graph uneditable. - Multiple Slides: Automatically placing multiple graphs on multiple slides.
- Auto-naming: Adding the date to the saved filename.
Summary
By using VBA, you can automate the task of pasting Excel charts into PowerPoint.
- It saves the trouble of manually copying and pasting graphs.
- Using a template ensures consistent formatting and design.
- It significantly improves efficiency in creating slides.
This is a very useful technique that can be applied to the automatic creation of presentation materials and routine reports.
