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
Related
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
I created a UserForm, with the help of many, within PowerPoint to assist with consistent embedded chart data entries. From what I have learned I will never do this again because addtional code that is needed to traverse through the PowerPoint to get to the correct location for the desired updates. No matter, I would like to finish the final objective of getting data from the UserForm into the first empty row. I have code but there is an error that is preventing the data to unload. The code I have is below. Any help in identifying the glitch is greatly appreciated.
Private Sub CPDataAdd_Click()
Dim sld As Slide
Dim shp As shape
Dim chrt As Chart
Dim xlWB As Object
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.Type = msoChart Then
If shp.Name = "DVchart" Then
Set xlWB = shp.Chart.ChartData.Workbook
Exit For
End If
End If
Next
If Not xlWB Is Nothing Then Exit For
Next
Set shp = sld.Shapes("DVchart")
Set xlWB = shp.Chart.ChartData.Workbook
Dim LastRow As Long
With xlWB.Sheets(1)
LastRow = .Range("AI" & Rows.Count).End(xlup).Row + 1
.Range("AI" & LastRow).Value = CPDate.Text
.Range("BI" & LastRow).Value = CPCompleteN.Text
.Range("CI" & LastRow).Value = CPPassN.Text
.Range("DI" & LastRow).Value = CPFailN.Text
.Range("EI" & LastRow).Value = CPNotN.Text
End With
End Sub
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.
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
I have a ppt which is generated every week. I have created a vbscript for updating the linked chart.. but i couldn't find how to identify the excel sheet which i have used for creating the table in the ppt...
Dim pptChart
Dim pptChartData
Dim xlWorkbook
Dim sld
Dim shp
'opent the ppt
strPresPath = "C:\oldpptlocation.pptx"
Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = True
Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
For Each sld In oPPTFile.Slides 'ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set pptChart = shp.Chart
Set pptChartData = pptChart.ChartData
pptChartData.Activate
Set pptWorkbook = pptChartData.Workbook
On Error Resume Next
'update first link
pptWorkbook.UpdateLink pptWorkbook.LinkSources(1)
'On Error GoTo 0
pptChart.Refresh
pptWorkbook.Close True
End If
Next
Next
oPPTFile.SaveAs ("C:\updated_ppt.pptx")
oPPTFile.Close
oPPTApp.Quit
Set pptWorkbook = Nothing
Set pptChartData = Nothing
Set pptChart = Nothing
You're currently checking each shape for a chart, you need to add an ElseIf to test whether the shape .HasTable.
If shp.HasChart Then
'your code to update chart
ElseIf shp.HasTable Then
'your code to update table
End If