Select all Tables in power point slide - vba

I am trying to create a macro which selects all the tables present in a slide in ppt using vba i tried but the macro is selecting the last table or the table created lastly
here is the code
Sub CheckCoOrdinates()
Dim pptPres As Presentation
Set pptPres = Application.ActivePresentation
Dim pptSlide As Slide
Dim pptShapes As Shape
For Each pptSlide In pptPres.Slides
For Each pptShapes In pptSlide.Shapes
If pptShapes.Type = msoTable Then
Dim i As Integer
For i = 1 To pptSlide.Shapes.Count
pptShapes.Select
pptShapes.Copy
Next
End If
Next
Next
how to create a macro for this

Instead of pptShapes.Select, use pptShapes.Select (False)
The default behavior of Select mimics clicking on a new shape ... the clicked shape is selected, replacing any previous selection. Adding the False parameter makes it behave more like Ctrl+clicking ... the newly selected shape is ADDED to the current selection.
That'll work on a per slide basis but you can't select shapes on multiple slides, so you're going to have to re-write your macro accordingly.
I suspect you'll be better off stepping through each slide, then through each shape on the slide and copy/pasting the tables one at a time.

Dim pptPres As Presentation
Set pptPres = Application.ActivePresentation
Dim xlApp As Object
Dim xlWorkBook As Object
Dim j As Integer
Dim r1 As String
j = 1
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("D:\Book2.xlsx", True, False)
Dim pptSlide As Slide
Dim pptShapes As Shape
For Each pptSlide In pptPres.Slides
For Each pptShapes In pptSlide.Shapes
If pptShapes.Type = msoTable Then
Dim i As Integer
For i = 1 To pptSlide.Shapes.Count
pptSlide.Select
pptShapes.Select 'msoFalse
pptShapes.Copy
xlWorkBook.sheets(1).Activate
r1 = "A" + CStr(j)
xlWorkBook.sheets(1).Range(r1).PasteSpecial Paste:=xlPasteValues
j = j + 20
Next
End If
Next
Next
'xlWorkBook.Close SaveChanges:=True
Set xlApp = Nothing
Set xlWorkBook = Nothing

Related

Powerpoint VBA copy text from tables

I am trying to create a macro which copies the text from all the tables in a slide. I can select the tables but failed to copy text entries from tables. I need to paste the copied text to a excel spreadsheet.
Here is the script:
Option Explicit
Sub GetTableNames()
Dim pptpres As Presentation
Set pptpres = ActivePresentation
Dim pptSlide As Slide
Set pptSlide = Application.ActiveWindow.View.Slide
Dim pptShapes As Shape
Dim pptTable As Table
For Each pptSlide In pptpres.Slides
For Each pptShapes In pptSlide.Shapes
If pptShapes.HasTable Then
Set pptTable = pptShapes.Table
pptShapes.Select msoFalse
pptShapes.TextFrame.TextRange.Copy
End If
Next
Next
End Sub
enter image description here
enter image description here
Try this code:
Sub GetTableNames()
Dim pptpres As Presentation
Set pptpres = ActivePresentation
Dim pptSlide As Slide
Set pptSlide = Application.ActiveWindow.View.Slide
Dim pptShapes As Shape, pptTable As Table
Dim XL As Object, WS As Object
Dim arr As Variant, nextTablePlace As Integer, cnt As Integer
Set XL = CreateObject("Excel.Application")
With XL.Workbooks.Add
Set WS = .Worksheets(1)
End With
nextTablePlace = 1 ' to output first table content into Worksheet
For Each pptSlide In pptpres.Slides
For Each pptShapes In pptSlide.Shapes
If pptShapes.HasTable Then
cnt = cnt + 1
Set pptTable = pptShapes.Table
WS.Cells(nextTablePlace, 1) = "Table #: " & cnt ' caption for each table
nextTablePlace = nextTablePlace + 1
ReDim arr(1 To pptTable.Rows.Count, 1 To pptTable.Columns.Count) ' resize array to table dimensions
For rr = 1 To pptTable.Rows.Count
For cc = 1 To pptTable.Columns.Count
arr(rr, cc) = pptTable.Cell(rr, cc).Shape.TextFrame.TextRange.Text 'get text from each cell into array
Next
Next
' flush the arr to Worksheet
WS.Cells(nextTablePlace, 1).Resize(pptTable.Rows.Count, pptTable.Columns.Count) = arr
' to next place with gap
nextTablePlace = nextTablePlace + pptTable.Rows.Count + 2
End If
Next
Next
XL.Visible = True
End Sub

Using Cells inside of Range isn't working? [duplicate]

This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Closed 5 years ago.
For some reason this isn't working:
.Range(Cells(1, 1), Cells(lRows, lCols)).Copy
Any ideas? It's on line 78
Option Explicit
Public Sub averageScoreRelay()
' 1. Run from PPT and open an Excel file
' 2. Start at slide 1 and find a box that contains the words "iq_", if it has those words then it will have numbers after it like so "iq_43" or "iq_43, iq_56, iq_72".
' 3. find those words and numbers in the opened Excel file. Needs to recognize that ", " means there is another entry.
' 3. Copy column containing words from ppt ie. "iq_43"
' 4. Paste a Table into ppt with those values
' 5. Do this for every slide
'Create variables
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim pptSlide As Slide
Dim Shpe As Shape
Dim pptText As String
Dim pptPres As Object
Dim iq_Array As Variant
Dim arrayLoop As Integer
Dim i As Integer
Dim myShape As Object
Dim colNumb As Integer
Dim size As Integer
Dim k As Integer
Dim vsblSld As Object
Dim lRows As Long
Dim lCols As Long
colNumb = 5 'Set #of columns in the workbook
' Create new excel instance and open relevant workbook
Set xlApp = New Excel.Application
'xlApp.Visible = True 'Make Excel visible
Set xlWB = xlApp.Workbooks.Open("C:\Users\pinlop\Desktop\Gate\Macro\averageScores\pptxlpratice\dummyavgscore.xlsx", True, False) 'Open relevant workbook
If xlWB Is Nothing Then ' may not need this if statement. check later.
MsgBox ("Error retrieving Average Score Report, Check file path")
Exit Sub
End If
xlWB.Worksheets.Add After:=xlWB.ActiveSheet
'Make pptPres the ppt active
Set pptPres = PowerPoint.ActivePresentation
'Loop through each pptSlide and check for IQ text box, grab avgScore values and create pptTable
For Each pptSlide In pptPres.Slides
'searches through shapes in the slide
For Each Shpe In pptSlide.Shapes
'Identify if there is text frame
k = 1
If Shpe.HasTextFrame Then
'Identify if there's text in text frame
If Shpe.TextFrame.HasText Then
pptText = Shpe.TextFrame.TextRange
If InStr(1, pptText, "iq_") > 0 Then 'Identify if within text there is "iq_" All IQ's have to be formatted like this "iq_42, iq_43" for now
iq_Array = Split(pptText, ", ") 'set iq_Array as an array of the split iq's
size = UBound(iq_Array) - LBound(iq_Array)
For arrayLoop = 0 To size 'loop for each iq_array
For i = 1 To colNumb 'loops for checking each column
If i = 1 And arrayLoop = 0 Then 'Copies the first column for every slide
xlWB.Worksheets("Sheet1").Columns(1).Copy 'copy column
xlWB.Worksheets("Sheet2").Paste Destination:=xlWB.Worksheets("Sheet2").Columns(1)
ElseIf xlWB.Worksheets("Sheet1").Cells(1, i) = iq_Array(arrayLoop) And i <> 1 Then 'if iq in ppt = iq in xl and if not the first cell then execute
k = k + 1
xlWB.Worksheets("Sheet1").Columns(i).Copy
xlWB.Worksheets("Sheet2").Paste Destination:=xlWB.Worksheets("Sheet2").Columns(k)
End If
Next i
Next arrayLoop
End If
End If
End If
Next Shpe
'calculate last row and last column
With xlWB.Worksheets("Sheet2")
lRows = .Cells(.Rows.Count, 1).End(xlUp).Row
lCols = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Range(Cells(1, 1), Cells(lRows, lCols)).Copy
End With
pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse
Set myShape = pptSlide.Shapes(pptSlide.Shapes.Count)
'Set position:
myShape.Left = 66
myShape.Top = 152
xlWB.Worksheets("Sheet2").Range("A1:P10").Clear
Next pptSlide
xlWB.Worksheets("Sheet2").Delete
End Sub
It should be like this:
.Range(.Cells(1, 1), .Cells(lRows, lCols)).Copy
That's one of the errors everyone experiences with VBA, if he goes a bit deeper. The reason is that Cells and Range should both be referred to the worksheet, otherwise they would refer the ActiveSheet.
And in general, consider using Long instead of Integer in your code.

Printing the contents from powerpoint in an Excel file using VBA

Im looking for a way to copy the text of the first element of a powerpoint slide into an excel file. I got the following code that prints out the text of the first box:
Sub getText
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
For Each sld In ActivePresentation.Slides
With sld.Shapes(1)
myInput = .TextFrame.TextRange.Text
MsgBox (myInput)
End With
Next
End sub
Now the next step I want to take is to add the data to an excel file. Therefore I try to do the following:
Sub getText()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("~\PROJECTEN\Lopend\office_VA\macroStore.xlsx", True, False)
xlWorkBook.sheets(1).Range("A2").Select
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
For Each sld In ActivePresentation.Slides
With sld.Shapes(1)
myInput = .TextFrame.TextRange.Text
ActiveCell.Text = myInput
End With
Next
End Sub
However when I try it now it get the error: "Object required". Any thoughts on how I should change my code?
Your problem is that you're referencing ActiveCell.Text but VBA has no clue what that is. Also, you haven't declared your myInput variable.
Try this macro, where instead of selecting the cell I'm just assigning the text value to it. Also, if you're writing more than one value your code will keep writing over the same cell. In the code below I've added a couple of lines that will write your text down column A.
Sub getText()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("~\PROJECTEN\Lopend\office_VA\macroStore.xlsx", True, False)
Dim xlWorkSheet As Object ' Create a worksheet object
xlWorkSheet = xlWorkBook.sheets(1) ' Set the sheet you activate to that object
Dim iRow As Long ' Create a variable to store row number
iRow = 1 'Set the first row that you want to start writing data on
Dim sld As Slide
Dim myInput As String
Set sld = Application.ActiveWindow.View.Slide
For Each sld In ActivePresentation.Slides
With sld.Shapes(1)
myInput = .TextFrame.TextRange.Text
xlWorkSheet.Cells(iRow, "A") = myInput 'Using .Cells() you can specify the (row, column) location
iRow = iRow + 1 'increment by one for next line of text
End With
Next
End Sub

Excel VBA, error 438 when adding a new slide to PPT

I'm trying to add a silde in existing powerpoint presentation for each chart in opened excel file. VBA keeps throwing errors.
In here pptApp.ActivePresentation.Add I keep getting an error that the Object does not support method
And in here ActiveChart.ChartArea.Copy that the object variable is not set.
Is is as hopeless as it appears?
Option Explicit
#Const EARLYBINDING = False
Sub CopyAndLinkAllChartsToExistingPPT()
#If EARLYBINDING Then
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
#Else
Dim pptApp As Object
Dim pptPres As Object
Dim pptSlide As Object
Const ppLayoutTitle = 1
#End If
Dim workS As Worksheet
Dim chartS As Excel.ChartObjects
Dim workS_Count As Integer
Dim chartS_Count As Integer
Dim W As Integer
Dim C As Integer
'Declaring PPT objects
Set pptApp = GetObject(, "PowerPoint.Application")
Set pptPres = pptApp.Presentations.Add(msoTrue)
Set pptSlide = pptPres.Slides.Add(1, ppLayoutTitle)
'Declaring Excel objects
Set workS = ActiveWorkbook.worksheets(1)
Set chartS = workS.ChartObjects
'Amount of worksheets and charts for the loops
workS_Count = ActiveWorkbook.worksheets.Count
chartS_Count = workS.ChartObjects.Count
'Nested loop for all the worksheets and charts
For W = 1 To workS_Count
For C = 1 To chartS_Count
pptApp.ActivePresentation.Add
pptApp.ActivePresentation.Slides.Count 1, ppLayoutTitle
pptApp.ActiveWindow.View.GotoSlide
pptApp.ActivePresentation.Slides.Count
Set pptSlide = pptApp.ActivePresentation.Slides(pptApp.ActivePresentation.Slides.Count)
chartS.Select
ActiveChart.ChartArea.Copy
'Pasting chart in PowerPoint slide with a data link
pptSlide.Shapes.PasteSpecial link:=msoTrue
Next C
Next W
' Clearing the objects
Set pptApp = Nothing
Set pptPres = Nothing
Set pptSlide = Nothing
Set workS = Nothing
Set chartS = Nothing
End Sub
I think that you got it all wrong there when you are adding slides.
You already created a presentation so you just need to add slides and paste charts right ?
What you are doing in your loop (and wrong) is adding a new presentation for each chart that you need to paste, then a slide to it.
Try to simplify it:
For W = 1 To workS_Count
For C = 1 To chartS_Count
Set pptSlide = pptPres.slides.add(pptPres.slides.count, ppLayoutTitle)
chartS(chartS_Count).Select
ActiveChart.ChartArea.Copy
'Pasting chart in PowerPoint slide with a data link
pptSlide.Shapes.PasteSpecial link:=msoTrue
Next C
Next W

VBA export multiple charts (4 each time) from the same sheet into one powerpoint slide

I've been trying to export multiple excel charts into powerpoint but there is a catch...I'd like to export 4 charts into a single slide at a time.
I've found the following code but it needs to be modify so that 4 charts are exported into one slide, instead of a single chart per slide.
The code is below:
Thanks!
Sub PushChartsToPPT()
Dim ppt As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSld As PowerPoint.Slide
Dim pptCL As PowerPoint.CustomLayout
Dim pptShp As PowerPoint.Shape
Dim cht As Chart
Dim ws As Worksheet
Dim i As Long
'Get the PowerPoint Application object:
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = msoTrue
Set pptPres = ppt.Presentations.Add
'Get a Custom Layout:
For Each pptCL In pptPres.SlideMaster.CustomLayouts
If pptCL.Name = "Title and Content" Then Exit For
Next pptCL
'Copy ALL charts embedded in EACH WorkSheet:
For Each ws In ActiveWorkbook.Worksheets
For i = 1 To ws.ChartObjects.Count
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select
For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
Set cht = ws.ChartObjects(i).Chart
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next i
Next ws
End Sub
Try this:
For Each ws In ActiveWorkbook.Worksheets
For i = 1 To ws.ChartObjects.Count Step 4 'your count must be a multiple of four other it wouldn't work
Set pptSld = pptPres.Slides.AddSlide(pptPres.Slides.Count + 1, pptCL)
pptSld.Select
For Each pptShp In pptSld.Shapes.Placeholders
If pptShp.PlaceholderFormat.Type = ppPlaceholderObject Then Exit For
Next pptShp
For j = 0 to 3
Set cht = ws.ChartObjects(i+j).Chart
cht.ChartArea.Copy
ppt.Activate
pptShp.Select
ppt.Windows(1).View.Paste
Next J
Next i