Excel VBA to update powerpoint text box - excel

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

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

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)

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

how to update a label caption in powerpoint from excel in vba

I need to update a label caption using Powerpoint object from excel macro. I cant able to update.
Dim objPpt As PowerPoint.Application
Dim objPre As Presentation
Dim objSlide As Slide
Dim objLayout As CustomLayout
Dim objTxtBox As Shape
Set objPpt = CreateObject("Powerpoint.Application")
Set objPre = objPpt.Presentations.Open("H:\Macro\SamplePpt.pptm")
ActivePresentation.Slides(1).lblHi.Caption = "Hello"
Set objSlide = objPre.Slides.Item(1)
Set objTxtBox = objSlide.Shapes.Item(1)
lblHi.Caption = "Hello"
Help me to do this....!!!!

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