Reference custom layout in PowerPoint from Excel by name - excel

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))

Related

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

delete pictures from slides in powerpoint from 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

Run time error '-2147188160 (80048240)': Application (unknown member) : Invalid request. sub or function not defined

I am getting this error while trying to run the following macro.
The error shows up on the first(!) time the "AddSlide" is executed and only then(!). The macro continues (after I click on "End" in the message box) and works well without any error message after that.
Sub PushChartsToPPT_1()
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptShp As PowerPoint.Shape
Dim EndTime As Single
Dim rng As Range
Dim cht As Chart
Dim ws As Worksheet
Dim i, j As Single
Dim MainWorkBook, tempWorkBook As Workbook
Dim tempSheet As Worksheet
Dim pptCL As CustomLayout
Dim myShape As Object
Dim DestinationPPT, str As String
Set MainWorkBook = ActiveWorkbook
'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
DestinationPPT = "Template.pptx"
Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only
'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL
ppt.Visible = msoTrue
For Each ws In MainWorkBook.Worksheets
For i = 1 To ws.ChartObjects.Count
'>>>>> error next line
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL) '>>>>>error on this line
pptSld.Select
Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
DoEvents
pptSld.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
Next i
Next ws
End Sub
Is it possible that the previous loop is finishing, instead of doing an Exit For at a 'title and content' slide? That would mean that the value pptCL is undefined. However, that would be the wrong error report; that line run with pptCL undefined causes an "invalid procedure call or argument".
But the odd thing is that your code died for me right on the line where the powerpoint file is opened, the line:
Set pptPres = ppt.Presentations.Open(DestinationPPT, True) ' read only
...and it died with that exact "automation error". I compared to code I have where a powerpoint file opened fine, and that code had the line:
ppt.Visible = msoTrue
...making the powerpoint application visible, just above the "open" command. When I added that line to your code, the error went away, in my run.
This may be a complete red herring, but since it worked for me, and since you aren't getting any other answers, take a moment and try it.

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