Excel to PowerPoint VBA - excel

I am trying to paste few cells from Excel to a slide in PowerPoint, using an ActiveX CheckBox in Excel as control. There is no problem of transferring one slide to my designated PowerPoint Presentation, but the problem appears when I ticked more than one box.
So what I do is basically making a temporary template presentation, and when I click on another button called the "Launch" button, it will be pasted to my designated presentation. This is my code:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Dim PPSlide As PowerPoint.Slide
Dim PPPres As PowerPoint.Presentation
Dim PP As PowerPoint.Application
Set PP = CreateObject("PowerPoint.Application")
Set PPPres = PP.Presentations.Open("(my temporary filename)")
Dim iCheckCount As Integer
iCheckCount = 0
Dim obj As OLEObject
For Each obj In ActiveSheet.OLEObjects
If obj.Object.Value = True Then iCheckCount = iCheckCount + 1
Next
If iCheckCount = 1 Then
Set PPSlide = PPPres.Slides(1)
With PPSlide
.Shapes("Textfeld 2").TextFrame.TextRange.Text = ActiveSheet.Range("G3").Text
.Shapes("Textfeld 3").TextFrame.TextRange.Text = ActiveSheet.Range("B3").Text
.Shapes("Textfeld 4").TextFrame.TextRange.Text = ActiveSheet.Range("C3").Text
.Shapes("Textfeld 5").TextFrame.TextRange.Text = ActiveSheet.Range("D3").Text
.Shapes("Textfeld 6").TextFrame.TextRange.Text = ActiveSheet.Range("F3").Text
End With
PPPres.Slides(1).Copy
Else
If iCheckCount > 1 Then
PPPres.Slides.Paste
PPPres.Slides(2).Copy
Set PPSlide = PPPres.Slides(1)
With PPSlide
.Shapes("Textfeld 2").TextFrame.TextRange.Text = ActiveSheet.Range("G3").Text
.Shapes("Textfeld 3").TextFrame.TextRange.Text = ActiveSheet.Range("B3").Text
.Shapes("Textfeld 4").TextFrame.TextRange.Text = ActiveSheet.Range("C3").Text
.Shapes("Textfeld 5").TextFrame.TextRange.Text = ActiveSheet.Range("D3").Text
.Shapes("Textfeld 6").TextFrame.TextRange.Text = ActiveSheet.Range("F3").Text
End With
End If
End If
End If
End Sub
I know that it won't work for more than 2 boxes (copied to designated Presentation). So my questions are:
1) How can you copy more than 1 Slide at once? I´ve tried
For i = 1 to PPPres.Slides.Count
PPPres.Slides.Item(i).Copy
Next i
but it won't work.
2) I found an if code for every ticked ActiveX CheckBox I have. But the problem is, how can I mention all the Sub for CheckBox_Click and ask the program to do it? The names of the Sub are Box1, Box2, Box3,...,Box46.
I know that my questions are really messy and I'm not explaining it very well since I am also new to VBA. Don't hesitate to ask me if you want to know more about my code.
Thankyou!

You may use e.g:
ActivePresentation.Slides.Range(Array(1, 2, 3)).Duplicate
' Or
For i = 1 to PPPres.Slides.Count
PPPres.Slides.Item(i).Duplicate
Next i

Related

Loop Through Columns in Existing Worksheet - Paste Values to Existing PowerPoint as Textboxes

Ive made a VBA macro that automatically creates a PowerPoint and one that creates a Worksheet named "Handlungsempfehlungen" with Text. The Worksheet "Handlungsempfehlungen" looks like this:
https://i.stack.imgur.com/nZEL8.png
It has about 40 columns (A-AO) and Text in each column from Row 1 to max. 34 (Number of rows filled with text varies each column). I now need to somehow loop through each row in each column and give each Cell.Value over to the existing (and currently opened) PowerPoint. Until now Ive used something like this to create textboxes in PowerPoint and fill them with Cell Values from Excel:
'New PPslide (copy slide 2 which is emtpy)
Set PPslide = PPapp.ActivePresentation.Slides(2).Duplicate.Item(1)
'Put new slide to end of PP
PPslide.MoveTo (PPpres.Slides.Count)
'Change title
PPslide.Shapes.Title.TextFrame.TextRange = "Slidetitle"
PPslide.Shapes(2).TextFrame.TextRange.Text = "Second title"
'Insert Textbox
Set PPtextbox = PPslide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=40, Top:=133, Width:=875, Height:=30)
PPtextbox.TextFrame.TextRange.Text = ActiveWorkbook.Worksheets("Handlungsempfehlungen").Cells(1, 1).Value
But with 40 columns and about 30 rows per column each filled with text I would need to create about 1000 textboxes and hand them to my PowerPoint. How could I loop through this Worksheet and automatically have positions on the PowerPoint Slide set for each textbox? The slidetitle for each PowerPointslide is already saved in the Row 35 of each Column in the Worksheet (see screenshot), so I would give this over to PP inside the loop as well (for each column set slidetitle = currentColumn.Row 35 is kinda the idea)
My current Idea for all of this is I having 5 textboxes per slide with set positions, filling them with the values from row 1-5 of the first column and then create a new slide and do the same process for rows 6-10 and so on until the Cell.Value in the current column is empty, then jump one column to the right and create a new PPslide again and repeat the whole process until the whole Worksheet has been worked through. I think this seems relatively simple but I am still a beginner and have difficulties implementing this.
Would this be a good idea and how would I need to get there? Im quite bad at looping but Im happy for every answer! Thanks for your time & help!
PS: the declarations for the created PP and its Objects:
Public Shape As Object
Public PPshape As PowerPoint.Shape
Public PPapp As PowerPoint.Application
Public PPpres As PowerPoint.Presentation
Public PPslide As PowerPoint.Slide
Public PPtextbox As PowerPoint.Shape
Set PPapp = New PowerPoint.Application
PPapp.Visible = msoTrue
The following code covers two scenarios:
You have PowerPoint open with an active presentation that has a slide at the begining with a Title and 5 texboxes properly named
You have PowerPoint closed
You need to set a reference to PowerPoint object model like this:
Read code's comments and try to adjust it to fit your needs
Use the F8 key to step into the code line by line
You can also add a Stop statement so the code breaks and then use the F8 key
Public Sub TransferDataToPPT()
' Set basic error handling
On Error GoTo CleanFail
' Turn off stuff
Application.ScreenUpdating = False
Dim pptApp As PowerPoint.Application
Dim pptPresentation As PowerPoint.Presentation
Dim pptMainSlide As PowerPoint.Slide
Dim pptContentSlide As PowerPoint.Slide
Dim isNewPPTInstance As Boolean
' Open and get PowerPoint instance
Set pptApp = OpenGetPowerPoint(isNewPPTInstance)
' If it's a new instance add new presentation and main slide
If isNewPPTInstance Then
pptApp.Visible = msoTrue
Set pptPresentation = pptApp.Presentations.Add(msoTrue)
Set pptMainSlide = pptPresentation.Slides.Add(1, ppLayoutTitleOnly)
pptMainSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 150, 100, 20).Name = "Textbox1"
pptMainSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 200, 100, 20).Name = "Textbox2"
pptMainSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 250, 100, 20).Name = "Textbox3"
pptMainSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 300, 100, 20).Name = "Textbox4"
pptMainSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 350, 100, 20).Name = "Textbox5"
Else
Set pptPresentation = pptApp.ActivePresentation
Set pptMainSlide = pptPresentation.Slides(1)
End If
' Set a reference to the sheet holding the values
Dim contentSheet As Worksheet
Set contentSheet = ThisWorkbook.Worksheets("Sheet1")
' Set the Excel range to be evaluated
Dim contentRange As Range
Set contentRange = contentSheet.Range("A1:AO34")
' Start a cell counter
Dim cellCounter As Long
cellCounter = 1
' Loop through columns and cells
Dim contentColumn As Range
Dim contentCell As Range
For Each contentColumn In contentRange.Columns
For Each contentCell In contentColumn.Cells
' Skip after first blank cell
If contentCell.Value = vbNullString Then Exit For
' Add new slide every 5 cells and fill title
If cellCounter = 1 Then
Set pptContentSlide = pptPresentation.Slides(1).Duplicate()(1)
pptContentSlide.MoveTo pptPresentation.Slides.Count
pptContentSlide.Shapes.Title.TextFrame.TextRange = contentSheet.Cells(35, contentColumn.Column).Value
End If
' Add value to textbox
pptContentSlide.Shapes("Textbox" & cellCounter).TextFrame.TextRange = contentCell.Value
cellCounter = cellCounter + 1
' Reset counter
If cellCounter > 5 Then cellCounter = 1
Next contentCell
Next contentColumn
CleanExit:
' Turn on stuff again
Application.ScreenUpdating = True
If isNewPPTInstance Then
If Not pptApp Is Nothing Then
pptPresentation.SaveAs "C:\Temp\NewPPT.pptx"
pptApp.Quit
End If
End If
Set pptApp = Nothing
Exit Sub
CleanFail:
MsgBox "An error occurred:" & Err.Description
GoTo CleanExit
End Sub
Private Function OpenGetPowerPoint(ByRef isNewPPTInstance As Boolean) As PowerPoint.Application
Dim pptApp As PowerPoint.Application
On Error Resume Next
Set pptApp = GetObject(, "PowerPoint.Application")
If pptApp Is Nothing Then
'PPT wasn't running, start it from code:
Set pptApp = CreateObject("PowerPoint.Application")
isNewPPTInstance = True
End If
Set OpenGetPowerPoint = pptApp
End Function
Let me know if it works

Excel to PowerPoint VBA Loop

Fist of all, I'm not good with VBA, and to be honest I don't know how to go about on this problem.
The Situation
I have a database as shown below with multiple entries (currently only 2, but there will be more as soon as the students will progress with there work). I want to be able to filter the database and then depending on the selection put the information in a PowerPoint Slide.
I created (with a lot of youtube videos) a script that will copy the relevant information from one line onto a PowerPoint presentation into the defined fields.
Problem
I have absolutely no idea how to loop that code in order to bring only the filtered Information onto the PowerPoint. Can someone guide me on how to go about it?
Sub XLS_to_PPT()
Dim pptPres As Presentation
Dim strPfad As String
Dim strPOTX As String
Dim pptApp As Object
strPfad = "C:XXX"
strPOTX = "PPT_Template.pptx"
Set pptApp = New PowerPoint.Application
pptVorlage = strPfad & strPOTX
pptApp.Presentations.Open Filename:=pptVorlage, untitled:=msoTrue
Set pptPres = pptApp.ActivePresentation
pptPres.Slides(1).Duplicate
pptPres.Slides(1).Select
pptPres.Slides(1).Shapes("Header").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(5, 5).Value
pptPres.Slides(1).Shapes("ClientChanlenge").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(5, 9).Value
pptPres.Slides(1).Shapes("HowWeHelped").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(5, 10).Value
pptPres.SaveAs strPfad & ("New_Request")
pptPres.Close
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
The plan is to create a new slide for each visible row in the table, i presume.
so you could loop through the table like this:
For Each tableRow In Sheets("NameOfYourSheet").ListObjects("NameOfYourTable").DataBodyRange.SpecialCells(xlCellTypeVisible).Rows
set newSlide = pptPres.Slides(1).Duplicate
newSlide.Shapes("Header").TextFrame.TextRange.Characters.Text = tableRow.Columns(5).Value
newSlide.Shapes("ClientChanlenge").TextFrame.TextRange.Characters.Text = tableRow.Columns(9).Value
newSlide.Shapes("HowWeHelped").TextFrame.TextRange.Characters.Text = tableRow.Columns(10).Value
Next tableRow
Basically we go through each row in the table, duplicate slide(1), use that new slide object to populate the shapes inside by the given column numbers.
SpecialCells(xlCellTypeVisible) takes care of ignoring the filtered out rows.
try looking into this answer.
There it is described how to loop through a filtered list. There is information there how to get the address of the cell in you are looping through and so on
edit: after I got reprimanded I am posting full solution. Hope it works.
edit2: now made it so it works for arbitrary number of slides
Sub XLS_to_PPT()
Dim pptPres As Presentation
Dim strPfad As String
Dim strPOTX As String
Dim pptApp As Object
strPfad = "C:XXX"
strPOTX = "PPT_Template.pptx"
Set pptApp = New PowerPoint.Application
pptVorlage = strPfad & strPOTX
pptApp.Presentations.Open Filename:=pptVorlage, untitled:=msoTrue
Set pptPres = pptApp.ActivePresentation
'below if set the range to 500 but you may want to increase /decrease that number depending on how many entries you expecty
Set rng = Range("A5:A500")
For Each cl In rng.SpecialCells(xlCellTypeVisible)
set mynewslide=pptPres.Slides(1).Duplicate
' I do not think you need below line
'pptPres.Slides(1).Select
mynewslide.Shapes("Header").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(cl.row, 5).Value
mynewslide.Shapes("ClientChanlenge").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(cl.row, 9).Value
mynewslide.Shapes("HowWeHelped").TextFrame.TextRange.Characters.Text = Worksheets("PPT_Creation").Cells(cl.row, 10).Value
Next cl
pptPres.SaveAs strPfad & ("New_Request")
pptPres.Close
Set pptPres = Nothing
Set pptApp = Nothing
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

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