Loop through presentations in current directory using Excel VBA - excel

The below code extracts data from the active PowerPoint presentation.
Sub ExportMultiplePowerPointSlidesToExcel()
'Declare our Variables
Dim ppApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim PPTTable As PowerPoint.Table
Dim PPTPlaceHolder As PowerPoint.PlaceholderFormat
'Declare Excel Variables.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWrkSheet As Excel.Worksheet
Dim xlRange As Excel.Range
'Grab the Currrent Presentation.
Set ppApp = GetObject(, "PowerPoint.Application")
Set PPTPres = ppApp.ActivePresentation
Set PPTSlide = PPTPres.Slides(1)
'Grab the Currrent Presentation.
'Keep going if there is an error
On Error Resume Next
'Get the Active instance of Outlook if there is one
Set xlApp = GetObject(, "Excel.Application")
'If Outlook isn't open then create a new instance of Outlook
If Err.Number = 429 Then
'Clear Error
Err.Clear
'Create a new Excel App.
Set xlApp = New Excel.Application
'Make sure it's visible.
xlApp.Visible = True
'Add a new workbook.
Set xlBook = xlApp.Workbooks.Add
'Add a new worksheet.
Set xlWrkSheet = xlBook.Worksheets.Add
End If
'Set the Workbook to the Active one, if Excel is already open. THIS ASSUMES WE HAVE A WORKBOOK IN THE EXCEL APP.
Set xlBook = Workbooks("Cycle 2 - FSO Dirs and EDs - CCG Talent Review template.xlsm")
'Set the Worksheet to the Active one, if Excel is already open. THIS ASSUMES WE HAVE A WORKSHEET IN THE WORKBOOK.
Set xlWrkSheet = xlBook.Worksheets("CCG list")
Range("B3").Value = PPTSlide.Shapes(4).TextFrame.TextRange
Range("E3").Value = PPTSlide.Shapes(7).TextFrame.TextRange
'Set the Worksheet Column Width.
xlWrkSheet.Columns.ColumnWidth = 20
'Set the Worksheet Row Height.
xlWrkSheet.Rows.RowHeight = 20
'Set the Horizontal Alignment so it's to the Left.
xlWrkSheet.Cells.HorizontalAlignment = xlLeft
'Turn off the Gridlines.
xlApp.ActiveWindow.DisplayGridlines = False
End Sub
How do I loop through the presentations in the current directory to perform the action?
I have attempted numerous methods but I can't seem to indicate each presentation. The name of the presentation should be irrelevant.

Related

Update existing tables in powerpoint with VBA Code

I am facing the following issue: I have an open excel-file as well as an open Powerpoint file. I want to select and copy two different ranges in Excel (range1: B4:D9, range 2: F4:H10") into two shapes (tables) in my powerpoint (range1 into Shape 16, range2 into Shape 20). When I run the subs for range1 and range2 in debug mode after each other everything works and the data is pasted correctly in Powerpoint. When I create one button for each range and use the button to execute the code its also working fine.
My problem: I would like to use the following code to call both codes for range1 and range2 to create only one button for this. When I run the sub below (UpdateSlide) the code pastes range F4:H10 into both Shapes 16 & 20 and I dont understand why. I am quite new to VBA so I appreciate every help to fix this. Many thanks in advance!
`Sub UpdateSlide()
Call range1
Call range2
End Sub`
Please see here for the two codes for range1 as well as range2:
`Sub range1()
'Range1 in Excel
Dim range1 As Excel.range
Dim sheet As Excel.Worksheet
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "Excel.Application")
Set sheet = excelApp.ActiveWorkbook.Sheets("Sheet1")
sheet.Activate
Set range1 = sheet.range("B4:D9")
range1.Select
range1.Copy
'Tabelle in Powerpoint auswählen
Dim table1 As Powerpoint.Shape
Dim pptApp As Powerpoint.Application
Set pptApp = GetObject(, "Powerpoint.Application")
pptApp.Activate
Dim slide As Powerpoint.slide
Set slide = pptApp.ActiveWindow.View.slide
Set table1 = slide.Shapes(16)
table1.table.Cell(1, 1).Select
'Daten einkopieren - ohne das Format verändert wird
pptApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
sheet.Activate
Set range1 = Nothing
Set sheet = Nothing
Set excelApp = Nothing
Set table1 = Nothing
Set pptApp = Nothing
Set slide = Nothing
End Sub
Sub range2()
'Range2 in Excel
Dim range2 As Excel.range
Dim sheet As Excel.Worksheet
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "Excel.Application")
Set sheet = excelApp.ActiveWorkbook.Sheets("Sheet1")
sheet.Activate
Set range2 = sheet.range("F4:H10")
range2.Select
range2.Copy
'Tabelle in Powerpoint auswählen
Dim table2 As Powerpoint.Shape
Dim pptApp As Powerpoint.Application
Set pptApp = GetObject(, "Powerpoint.Application")
pptApp.Activate
Dim slide As Powerpoint.slide
Set slide = pptApp.ActiveWindow.View.slide
Set table2 = slide.Shapes(20)
table2.table.Cell(1, 1).Select
'Daten einkopieren - ohne das Format verändert wird
pptApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
Set range2 = Nothing
Set sheet = Nothing
Set excelApp = Nothing
Set table2 = Nothing
Set pptApp = Nothing
Set slide = Nothing
End Sub`

Get data from Powerpoint Chart to Excel Worksheet using VBA

So when you right click a chart in Powerpoint and click Edit Data. A workbook will open up. I just want those data to be copied to my Excel file. Help me to extract every chart in each slide of powerpoint. Please help me Here's my code in PPT VBA so far:
Sub PowerpointToExcel()
Dim PPTPres As Presentation
Dim PPTSlide As Slide
Dim PPTShape As Shape
Dim PPTTable As Table
Dim PPTChart As Chart
Dim PPTPlaceHolder As PlaceholderFormat
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim PPTChartData As MSForms.DataObject
Set PPTPres = Application.ActivePresentation
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
Set xlBook = xlApp.Workbooks("Book2.xlsx")
Set xlSheet = xlBook.Worksheets("Sheet2")
Set PPTChartData = New MSForms.DataObject
For Each PPTSlide In PPTPres.Slides
For Each PPTShape In PPTSlide.Shapes
If PPTShape.HasChart Then
Set PPTChart = PPTShape.Chart
Set xlRange = xlSheet.Range("A10000").End(xlUp)
If xlRange.Value <> "" Then
Set xlRange = xlRange.Offset(1, 0)
End If
With PPTPres.Slides(PPTSlide).Shapes(PPTShape).Chart.ChartData
.Activate
.Workbook.Sheets(1).Range("A2:E10").Copy
PPTChartData.GetFromClipboard
End With
SData = PPTChartData.GetText(1)
xlRange.Value = SData
xlRange.Offset(0, 1).Value = PPTSlide.Name
xlRange.Offset(0, 2).Value = PPTChart.ChartData
End If
Next
Next
End Sub

Creating PPT from two different excel file in VBA

I am trying to open a excel file, take the ranges as picture and create a ppt our of it. I am not sure what is going wrong in the code below. Can someone help please? The code works well for all other slides coming from other excel file. The moment I opena excel file and switch , it is throwing "subscript out of range error"
Sub GlobalBankPPT()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppShape As PowerPoint.Shape
Dim ppPic As Variant
Dim j As Integer
Dim wkb As Workbook
Dim sh As Worksheet
Dim WS_Count As Integer, i As Integer
Dim mypp As New ExcelToPPt.cPPt
mypp.InitFromTemplate MyTemplateppt:=mytemp, MyOutputppt:=""
Set ppApp = GetObject(, "PowerPoint.Application")
ppApp.Visible = msoTrue
Set ppPres = ppApp.ActivePresentation
Set wkb = Workbooks.Open(Filename:=ThisWorkbook.Worksheets("Index").Range ("FilePath"))
wkb.Activate
WS_Count = wkb.Worksheets.Count
For i = 1 To WS_Count
If Worksheets(i).Name = "Industry" Then
wkb.Activate
Set sh = ActiveSheet
Exit For
End If
Next I
'--------------------------------------------------------------------Slide7
Range(Sheet15.Range("A150"), Sheet15.Range("Q191")).Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ppPres.Slides(7).Select
Set ppPic = ppPres.Slides(7).Shapes.Paste
ppPic.Select
ppPic.Top = 70.24
ppPic.Width = 10.5 * 100
ppPic.Height = ppPic.Width / 3.4
ppPic.Left = 50
end sub

How to update powerpoint table from excel workbook

I'm trying to write a code to run from excel, and then open an existing excel and powerpoint file from the c drive, and use the data in the worksheet named "Oct18" to update the FIRST table in Slide 2 of the powerpoint.
The following is the code I wrote, but somehow it doesn't work.
May I know if anyone knows how to go about this please ?
Sub WriteText_toPPT_Table()
'Add a reference to Microsoft Powerpoint 12.0 object library
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShp As PowerPoint.Shape
Dim ppSld As PowerPoint.slide
Dim xlworkbook As Workbook
Dim firstsheet As String
Dim fileDir As String
Dim excelFile As String
firstsheet = "Oct18"
'fileDir = "c:\masterpresentation.pptx"
'excelFile = "c:\masterexcel.pptx
'1) Open powerpoint application
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = msoTrue
Set xlworkbook = Excel.Application.Workbooks.Open(Filename:=excelFile)
'2) opening an existing presentation
Set ppPres = ppApp.Presentations.Open(Filename:=fileDir)
Set exceldir = Excel.Application.Workbooks.Open(Filename:=excelFile)
ppPres.Slides(2).Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = xlworkbook.Sheets(firstsheet).Cells(4, 12)
End Sub

How to open an existing Excel file?

I am copying a table from Outlook to Excel. The code I have found online copies the table in a new Excel file.
I want to copy the table into an existing Excel file.
Here is the code I am running in Outlook.
Sub dd()
Dim item As MailItem, x%
Dim r As Object 'As Word.Range
Dim doc As Object 'As Word.Document
Dim xlApp As Object, wkb As Object
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True
Dim wks As Object
Set wks = wkb.Sheets(1)
For Each item In Application.ActiveExplorer.Selection
Set doc = item.GetInspector.WordEditor
For x = 1 To doc.Tables.Count
Set r = doc.Tables(x)
r.Range.Copy
wks.Paste
wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
Next
Next
End Sub
The code here
Set wkb = xlApp.Workbooks.Add
is what opens the new workbook. Try replacing this line with something like
Set wkb = xlApp.Workbooks.Open("C:\PathToExcel\File.xlsx")

Resources