delete pictures from slides in powerpoint from excel - excel

I tried to figured out why the coding is not working, but the following code was supposed to open from excel a powerpoint and clear the existing slides in order to replace with new picture - however I'm getting the following:
error 91: Object variable or with block variable not set.
I tried several others code from the Stack but cannot make it work.. any help please? The deck contains slide 2 to slide 9 to cleared out.
Sub ppt_export()
Dim DestinationPPT As String
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim objApp As Object, objSlide As Object, ObjShp As Object, objTable As` Object
DestinationPPT = "C:\Users\\Desktop\Summary.pptx"
Set ppApp = CreateObject("PowerPoint.Application")
Set ppPres = ppApp.Presentations.Open(DestinationPPT)
'delete the shapes from the renew the template
For i = ppSlide.Shapes.Count To 1 Step -1
Set ppShape = ppSlide.Shapes(p)
If ppShape.Type = msoPicture Then ppShape.Delete
Next
End Sub
I'd like to know how to correct the code in order to continue the coding with copying excel worksheets as pictures into the respective slide.

First and most importantly, add Option Explicit to the top of the code module, and it will flag the various undeclared variables you have: p, i, ppSlide, and ppShape.
Then the code might look something like this:
Option Explicit
Sub ExportToPPT()
Dim ppApp As PowerPoint.Application
Set ppApp = New PowerPoint.Application
Dim ppFileName As String
ppFileName = "C:\Users\\Desktop\Summary.pptx"
Dim ppPres As PowerPoint.Presentation
Set ppPres = ppApp.Presentations.Open(Filename:=ppFileName)
Dim ppSlide As PowerPoint.Slide
Dim i As Integer
For i = 2 To 9
Set ppSlide = ppPres.Slides(i)
Dim j As Integer
For j = ppSlide.Shapes.Count To 1 Step -1
If ppSlide.Shapes(j).Type = msoPicture Then
ppSlide.Shapes(j).Delete
End If
Next j
Next i
End Sub

Related

Reference custom layout in PowerPoint from Excel by name

I'm in Excel and I'd like to reference a custom layout for a slide in PowerPoint by name. You can only refer to them by index, so I thought a function should do the trick:
Sub Monatsbericht()
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = New PowerPoint.Application
DestinationPPT = "C:\VBA\Reports\MonthlyReport_Template.pptm"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Debug.Print PPLayout("CLayout1")
'Rest of code
End Sub
Function PPLayout(clayout As String)
Dim myPresentation As PowerPoint.Presentation
Dim olay As PowerPoint.CustomLayout
For Each olay In ActivePresentation.SlideMaster.CustomLayouts
If olay.Name = clayout Then
PPLayout = olay.Index
Exit Function
End If
Next olay
End Function
I get error 429: "Object creation by Activex component not possible.", highlighting the for each line in the function.
Actually ActivePresentation should be myPresentation, Excel should not know the ActivePresentation. Also you must submit myPresentation as a parameter otherwise this is an empty variable in your function.
If you have a look at the Slides.AddSlide method (PowerPoint) you see that the second parameter is not an index but of type CustomLayout so your function must return the layout instead of an index.
Public Function PPLayout(clayout As String, myPresentation As PowerPoint.Presentation) As PowerPoint.CustomLayout
Dim olay As PowerPoint.CustomLayout
For Each olay In myPresentation.SlideMaster.CustomLayouts
If olay.Name = clayout Then
Set PPLayout = olay
Exit Function
End If
Next olay
End Function
And use it like
Debug.Print PPLayout("CLayout1", myPresentation).Index
or
myPresentation.Slides.AddSlide(myPresentation.Slides.Count + 1, PPLayout("CLayout1", myPresentation))

Issues with VBA constants

Earlier today I had a run time error 448 (named object not found) with the following code, written in Excel:
Sub PPTextbox()
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As Object
Dim DestinationPPT As String
Set PowerPointApp = CreateObject("PowerPoint.Application")
DestinationPPT = "H:\VBA\Kapitalanlageplanung - Präsentationen\Monatsbericht\MonatsberichtTemplate.pptm"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Set mySlide = myPresentation.Slides.Add(myPresentation.Slides.Count + 1, 12)
mySlide.Shapes.AddTextbox(Type:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=200, Height:=50).TextFrame.TextRange.Text = "Test Box"
End Sub
Turns out, the issue was Type:=msoTextOrientationHorizontal, replacing it with a simple 1 did the trick.
This comment I found gave me the solution. I know now I used late-binding by declaring mySlide as an Object. I know now it is inefficient and obviously can lead to some problems like I encountered. But why? Is there some logic behind it or do I just have to accept that "some of VBA constants are not recognised and they are treated as variables" when late-binding? Also, is that a random occurrence because the exact same code worked earlier?
I always use late-binding so that my code will run on other PCs without activating the dependencies. Portability is critical. I like to then define the constants that would be set by early binding manually.
Const msoTextOrientationHorizontal = 1
Sub PPTextbox()
Dim PowerPointApp As Object
Set PowerPointApp = CreateObject("PowerPoint.Application")
Dim DestinationPPT As String
DestinationPPT = "H:\VBA\Kapitalanlageplanung - Präsentationen\Monatsbericht\MonatsberichtTemplate.pptm"
Dim myPresentation As Object
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Dim mySlide As Object
Set mySlide = myPresentation.Slides.Add(myPresentation.Slides.Count + 1, 12)
mySlide.Shapes.AddTextbox(Type:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=200, Height:=50).TextFrame.TextRange.Text = "Test Box"
End Sub

Reading data from a table does not work in particular table

I am currently trying to create a new PPT presentation by deleting unwanted slides from a presentation. The slides - and their slide numbers - are being selected in the first column of a table I have in Excel.
I tried to solve this problem by taking another table instead of the one I want to use and it worked. For some reason it seems not work with the "Table 3".
Sub CreatingNewPresentation()
Dim Destination1PPT As String
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim myTable As ListObject
Dim TempArray As Variant
Dim x As Long
If MsgBox("This can take a while", vbOKCancel + vbExclamation, "Creating new presentation") = vbCancel Then
Exit Sub
Else
Set ppApp = CreateObject("PowerPoint.Application")
Destination1PPT = "C:\Users\Steffen\Desktop\Test2\1.pptx"
Set ppPres = ppApp.Presentations.Open(Destination1PPT)
ppApp.Visible = True
ppApp.Activate
Set myTable = ActiveSheet.ListObjects("Table3")
TempArray = myTable.ListColumns(1).DataBodyRange
For x = ppApp.ActivePresentation.Slides.Count To 1 Step -1
If IsError(Application.Match(x, TempArray, False)) Then
ppApp.ActivePresentation.Slides(x).Delete
End If
Next
End If
End Sub
I expect the code to open the presentation and delete all slides except the ones I store in "Table3" - column 1.
What it does instead is just open the presentation and nothing else. There is no error message.
I found the "stupid" mistake I made. I was getting ALL data entries of the referred table instead only getting the visible data entries.
This helps:
Set myTable = ThisWorkbook.Sheets("Sheet1").ListObjects("Table3")
TempArray = myTable.ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible)

Excel VBA to update powerpoint text box

With the help of below coding, I am able to open the powerpoint file but it is not updating the textbox.
I am getting an error as "Object variable or with block variable not set".
Dim PPT As PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
Set pres = PPT.Presentations.Open( _
"C:\Users\GShaikh\Desktop\Process Coach certificate template.pptx")
slideCtr = 1
Set tb = newslide.Shapes("TextBox1")
tb.TextFrame2.TextRange.Characters.Text = "OK"
Try:
PPT.ActivePresentation.Slides(2).Shapes("TextBox1").TextFrame.TextRange.Characters.Text = "qwerty"
To get the name of the shape just right (in an input box so that you can copy it), select it and run:
a = InputBox("The name of the selected shape is:", "Name of the Shape", PPT.ActiveWindow.Selection.ShapeRange.Name)
to change it, while you have it selected, try:
PPT.ActiveWindow.Selection.ShapeRange.Name = "TextBox2"
Hope this helps.
Error is taking place on the Set tb line because you never initialize newslide, or at least you don't show it here.
Assuming your text box is on slide one you can do something like the following (add before Set tb):
Set newslide = pres.Slides(1)
Also make sure the text box you want is actually "TextBox1". By default the names usually have a space before the number like "TextBox 1".
I tested your code with this change to verify it works. Full code here:
Sub test()
Dim PPT As PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim newslide As PowerPoint.Slide
Dim slideCtr As Integer
Dim tb As PowerPoint.Shape
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
Set pres = PPT.Presentations.Open( _
"C:\Users\GShaikh\Desktop\Process Coach certificate template.pptx")
slideCtr = 1
Set newslide = pres.Slides(1)
Set tb = newslide.Shapes("TextBox1")
tb.TextFrame2.TextRange.Characters.Text = "OK"
End Sub

Copy more excel shapes into powerpoint

I have the following code which let me copy
Sub pptCopy()
Dim pptapp As PowerPoint.Application
Dim ppt As PowerPoint.Presentation
Dim slide As PowerPoint.slide
Dim shape1 As PowerPoint.shape
var2 = "C:\Documents and Settings\aa471714\Desktop\Presentation2.pot"
Set pptapp = CreateObject("Powerpoint.Application")
Set ppt = pptapp.Presentations.Open(var2)
Set slide = ppt.Slides(2)
Set shape1 = slide.Shapes.Paste(1)
pptapp.Visible = True
Call copyExcel1
With shape1
.Left = 100
.Width = 100
End With
End Sub
And an another macro
Sub copyExcel1()
Dim oExcel As Excel.Application
Dim oWB As Workbook
Dim cht As Excel.ChartObject
var2 = "C:\Documents and Settings\aa471714\Desktop\Template.xls"
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open(var2)
oExcel.Visible = True
Sheets("Sheet5").ChartObjects("AchmeaBankNL").Chart.ChartArea.Copy
End Sub
I however have two issues:
I want to move the excel picture to a specific place in powerpoint (fe shape 3)
I want to copy more than excel figure (i need to copy 2, one to shape 3 and one to shape 4).
Does anybody know how I should edit code below to accomplish this?
Dear regards,
Marc
First, you should edit the subject of your question; it doesn't seem to have anything to do with the question itself.
To move the shape to a specific position in the "stacking" order, use something like this function:
Function ShapeToZOrder(oSh As Shape, lPosition As Long)
oSh.ZOrder msoSendToBack
Do Until oSh.ZOrderPosition = lPosition
oSh.ZOrder msoBringForward
Loop
End Function

Resources