Home > Back-end >  How can I get data from powerpoint slide master
How can I get data from powerpoint slide master

Time:09-16

With the following code, I'm able to collect data from a slide and sent them to an excel sheet.

Unfortunately, I have some data that are writen in one of the layout of the slide master. Do you know how can I access to those shapes and collect them in the same excel sheet?

Sub ExportMultiplePowerPointSlidesToExcel()

Dim PPTPres As Presentation

Dim PPTSlide As Slide
Dim PPTShape As Shape

Dim PPTTable As Table
Dim PPTPlaceHolder As PlaceholderFormat

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 PPTPres = Application.ActivePresentation
                         
    '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 
Set xlBook = xlApp.Workbooks("ExportFromPowerPointToExcel.xlsm")
        
'Set the Worksheet to the Active one, if Excel is already open.
Set xlWrkSheet = xlBook.Worksheets("Slide_Export")
        
'Loop through each Slide in the Presentation.
Set PPTSlide = ActivePresentation.Slides(1)
        
'Loop through each Shape in Slide.
For Each PPTShape In PPTSlide.Shapes
                
'If the Shape is a Table.
     If PPTShape.Type = msoPlaceholder Or PPTShape.Type = ppPlaceholderVerticalObject Then
                    
'Grab the Last Row.
        Set xlRange = xlWrkSheet.Range("A20").End(xlUp)
    
'Handle the loops that come after the first, where we need to offset.
        If xlRange.Value <> "" Then
    
'Offset by One rows.
           Set xlRange = xlRange.Offset(1, 0)
    
        End If

'Grab different Shape Info and export it to Excel.
                   xlRange.Value = PPTShape.TextFrame.TextRange
                                                  
                End If
            Next
        
      
xlWrkSheet.Columns.ColumnWidth = 20    
xlWrkSheet.Rows.RowHeight = 20
xlWrkSheet.Cells.HorizontalAlignment = xlLeft
xlApp.ActiveWindow.DisplayGridLines = False
        
End Sub

Thank you for your precious help

regards,

nicolas

CodePudding user response:

You can access the master slide with the property SlideMaster. The data type is Master (not Slide). It has a Shape collection like a regular slide.

Dim PPTMaster As Master
Set PPTMaster = ActivePresentation.SlideMaster
For Each PPTShape In PPTMaster.Shapes
    (...)
Next

If you want to avoid to duplicate your code that handles the shapes, put that part into a subroutine that gets either a single Shape or a Shape-Collection as parameter.

CodePudding user response:

To get content from a particular layout:

Dim objLayout As CustomLayout

For Each objLayout In ActivePresentation.Designs(1).SlideMaster.CustomLayouts
    If objLayout.Name = "Layout Name" Then
        'Get the information here.
    End If
Next objLayout

Even though the property name is CustomLayouts, the same code works for built-in Microsoft slide layouts as well.

  • Related