I've made a code to open Powerpoint from excel, and loop through all the slides, find the graph and change some columns. I have the code for doing the replacement but cant' seem to loop through the slides because it throws an ActiveX error 429 that is basically saying that powerpoint is not found :O.
Sub pptDataChange()
'Define variables of excel
Dim mySheet As Excel.Worksheet
'Define variables to open on PPT
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim sld As Slide
Dim shp As Shape
Dim chrt As Chart
'Copy range from Excel
Set mySheet = ThisWorkbook.Worksheets("Sheet1")
'Create instance of Powerpoint
On Error Resume Next
'Open Powerpoint with Powerpoint is already opened
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear errors
Err.Clear
'If Powerpoint is closed, open Powerpoint
If PowerPointApp Is Nothing Then
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
End If
'Handle error if Powerpoint isn't installed or not found
If Err.Number = 429 Then
MsgBox ("PowerPoint not found, aborting...")
Exit Sub
End If
On Error GoTo 0
'Make Powerpoint visible and active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Open Powerpoint Presentation from PATH and set it as the active
PowerPointApp.Presentations.Open ("File.pptx")
For Each sld In ActivePresentation.Slides
For Each shp in sld
'Iterate through charts and change data of chart using something like If sld Has.Chart Then ...
Next sld
Exit Sub
What I'm thinking is that maybe it's because of the ActivePresentation, but I've tried referencing to myPresentation but It was the same.
Can you please help?
I explicitly declared a Presentation variable to use instead of ActivePresentation and that seemed to do the trick. FYI: I also changed the declarations for the sld and shp variables to explicitly reference the PowerPoint object library.
Sub pptDataChange()
'Define variables of excel
Dim mySheet As Excel.Worksheet
'Define variables to open on PPT
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim chrt As Chart
'Copy range from Excel
Set mySheet = ThisWorkbook.Worksheets("Sheet1")
'Create instance of Powerpoint
On Error Resume Next
'Open Powerpoint with Powerpoint is already opened
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear errors
Err.Clear
'If Powerpoint is closed, open Powerpoint
If PowerPointApp Is Nothing Then
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
End If
'Handle error if Powerpoint isn't installed or not found
If Err.Number = 429 Then
MsgBox ("PowerPoint not found, aborting...")
Exit Sub
End If
On Error GoTo 0
'Make Powerpoint visible and active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Open Powerpoint Presentation from PATH and set it as the active
Dim pres As PowerPoint.Presentation
Set pres = PowerPointApp.Presentations.Open("File.pptx")
For Each sld In pres.Slides
For Each shp In sld.Shapes
'Iterate through charts and change data of chart using something like If sld Has.Chart Then ...
Next shp
Next sld
End Sub
Related
I try to apply a powerpoint table style from vba excel but i had an issue, someone can help to try how apply a style in table object ?
My macro work fine expect the style part ....
this code go to error 400 -- execution error --> Method 'table' of the object 'Shape' has failed
Set otbl = PPT_Shape.Table
This is a sample of my code here below
Sub ExcelRangeToPowerPoint()
'PURPOSE: Copy/Paste An Excel Range Into a New PowerPoint Presentation
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim sPath As String
Dim project As String
Dim otbl As TableObject
'Set Title
project = Feuil1.Cells("2", "C")
'Set the template
sPath = "C:\Users\E049XXXX\OneDrive - XXX\XXX\"
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:N34")
'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
'Add a slide to the Presentation
PowerPointApp.ActivePresentation.ApplyTemplate "C:\Users\E049XXXX\OneDrive - XXXX\Documents\XXXXX.thmx"
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=0 '0 = ppPasteDefault
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 66
myShape.Top = 152
'Add Title
Set myTitle = mySlide.Shapes.Title
myTitle.TextFrame.TextRange.Characters.Text = project
'Add style
Set PPT_Shape = myShape
Set otbl = PPT_Shape.Table
With otbl
.ApplyStyle "{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}", True
End With
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
I have a VBA code which is supposed to replace a word by another in a powerpoint. Nevertheless, when I try to run it I get the error 429 "ActiveX component can't create object" but I do not know why. Here is the code :
Sub pres()
Dim PowerPointApp As Object
Set PowerPointApp = CreateObject("PowerPoint.Application")
Dim myPres As Object
Set myPres = PowerPointApp.Presentations.Open("C:\Users\NAME\Desktop\PRESVBA\Pres.pptx")
Dim sld As Slide
Set sld = ActivePresentation.Slides(3)
Dim shp As Shape
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
End If
End If
Next shp
End Sub
I think that you are trying to update the slides from excel, if so please follow the steps
In the excel vba, go to Tools > References > check for Microsoft PowerPoint 16.0 Object Library (for Office 2019), you will find similar if the version of the Office is lower. Click OK
The code below
Sub pres()
Dim PowerPointApp As Object
Set PowerPointApp = CreateObject("PowerPoint.Application")
Dim myPres As Object
Set myPres = PowerPointApp.Presentations.Open("C:\Personal\test\Presentation1.pptx")
Dim sld As PowerPoint.Slide '<- change here
Set sld = myPres.Slides(1) '<- number in brackets indicate the slide number
Dim shp As PowerPoint.Shape '<- change here
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
End If
End If
Next shp
End Sub
Please mark it as an answer if it solves the pain :)
I want to create a macro which opens an existing powerpoint template for me, copy data from a certain sheet of excel and then paste it in a specific slide in powerpoint.
I tried googling it online and created something but it doesnt work. The macro runs but I do not see any output. Please help. Below is the code I am working on:
Sub Excelrangetopowerpoint()
Dim rng As Range
Dim Powerpointapp As PowerPoint.Application
Dim myPresentation As PowerPoint.Application
Dim DestinationPPT As String
Dim myShape As Object
Dim myslide As Object
Set rng = Worksheets("regions").Range("B1:N18")
On Error Resume Next
Set Powerpointapp = CreateObject("Powerpoint.application")
DestinationPPT = ("C:\Users\OLX-Admin\Dropbox (Corporate Finance)\Naspers Monthly Reporting\Prep for call\From teams\FY2019\OLX Group Monthly Report_Sep'18_Macro.pptx")
Powerpointapp.Presentations.Open (DestinationPPT)
If Err.Number = 429 Then
MsgBox "Powerpoint could not be found.aborting."
Exit Sub
On Error GoTo 0
Application.ScreenUpdating = False
rng.Copy
Set myslide = PowerPoint.ActivePresentation.Slides(4)
myslide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = myslide.Shapes(myslide.Shapes.Count)
myShape.Left = 152
myShape.Top = 152
Powerpointapp.Visible = True
Powerpointapp.Activate
activation.CutCopyMode = False
End If
End Sub
This should work.
There were some missing parts of your modified code.
Notice that if the presentation is already open, this code will open the existing file in "read-only" mode... (so it doesn't care if powerpoint file is already opened or not).
VBA Code
Sub Excelrangetopowerpoint()
Dim rng As Range
Dim Powerpointapp As Object
Dim myPresentation As Object
Dim DestinationPPT As String
Dim myShape As Object
Dim mySlide As Object
'Copy Range from Excel
Set rng = Worksheets("regions").Range("B1:N18")
'Create an Instance of PowerPoint
On Error Resume Next
'Set your destination path for the powerpoint presentation and open the file
Set Powerpointapp = CreateObject("Powerpoint.application")
DestinationPPT = ("C:\Test\My Powerpoint\Presentation1.pptx")
Powerpointapp.Presentations.Open (DestinationPPT)
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "Powerpoint could not be found.aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Set my current Powerpoint window as activated
Set myPresentation = Powerpointapp.ActivePresentation
'Set which slide to paste into
Set mySlide = myPresentation.Slides(4)
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 152
myShape.Top = 152
'Make PowerPoint Visible and Active
Powerpointapp.Visible = True
Powerpointapp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
Source: The code is a combination of the work by Chris Newman: "Copy & Paste An Excel Range Into PowerPoint With VBA" & "Copy & Paste Multiple Excel Ranges To Separate PowerPoint Slides With VBA" but with the modification that you add a path to an already existing PowerPoint file.
you were getting that error because of the below line.
Set myslide = PowerPoint.ActivePresentation.Slides(4)
The correct code is
Set myslide = PowerPointapp.ActivePresentation.Slides(4)
I'm writing some code to automatically copy some of the charts from an excel to a ppt. The first issue that I am facing is with the variable declaration
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
The error is "User defined-type not defined". Just to let you know I'm pretty new to this VBA, so some descriptive comments would be really helpful.
the error you are getting is because your powerpoint variables want to be defined as objects, and the objects set to be powerpoint applications later on.
Sub ChartX2P()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
If ActiveChart Is Nothing Then
MsgBox "Hey, please select a chart first."
Exit Sub
End If
If PowerPointApp Is Nothing Then _
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
On Error GoTo 0
Application.ScreenUpdating = False
'this will create a new powerpoint for your chart
Set myPresentation = PowerPointApp.Presentations.Add
'this will open an old powerpoint up, just change "File address" to the address
'Set myPresentation = PowerPointApp.Presentations.Open(Filename:="FileAddress")
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
ActiveChart.ChartArea.Copy
mySlide.Shapes.Paste
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShape.Left = 200
myShape.Top = 200
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
This is taken from here, but it looks pretty straightforward, anything you can't get from the site let me know
I have a chart range copied from excel. Now I want to paste it as picture in slide 5 of active presentation, but it is giving me below error.
"Shapes(unknown member):Invalid request.Clipboard is empty or contains data which may not be pasted here."
Please help, code below.
Sub UpdateCharts()
Dim oPP As PowerPoint.Slide
Dim shp As PowerPoint.shape
ActivePresentation.Slides(5).Shapes.Paste
End Sub
Try the code below (explanation inside the code comments):
Option Explicit
'=====================================================================
' This sub exports a range from Excel to PowerPoint,
' pastes it, and later on modifies it's position and size (if needed)
' The code works using Late-Binding, so there's no need
' to add References to the VB Project
'=====================================================================
Sub UpdateCharts()
Dim ppApp As Object
Dim ppPres As Object
Dim ppSlide As Object
Dim ppShape As Object
' check if PowerPoint application is Open
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If ppApp Is Nothing Then
MsgBox "PowerPoint Application is not open", vbCritical
Exit Sub
End If
' set the Active Presentation
Set ppPres = ppApp.ActivePresentation
' set the Slide
Set ppSlide = ppPres.Slides(5)
' --- copy the Chart's Range (from Excel) ---
' <-- put your copy section here, right before the Paste
'With Worksheets("toPPT")
' .Range("F6:J7").Copy
'End With
' --- Paste to PowerPoint and modify properties (if needed) ---
Set ppShape = ppSlide.Shapes.PasteSpecial(3, msoFalse) ' 3 = ppPasteMetafilePicture
' modify properties of the pasted Chart (if needed)
With ppShape
.Left = 535
.Top = 86
End With
Application.CutCopyMode = False
ppPres.Save
Set ppSlide = Nothing
Set ppPres = Nothing
Set ppApp = Nothing
End Sub
Try this:
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.(5))