Search code examples
vbapowerpointcell

Insert specific slides from other presentations to current on based on input provided in table


I am quite new to VBA (less than a week old). I am trying to create a macro that will insert slides in the current presentation based on file path and slide range provided in a table on Slide 1. I have created the following code but I think I am doing something wrong since it doesn't work. Please help ...

Sub Insert_Slides()

Dim sl As Slide
Dim tbl As Table
Dim shp As Shape

Dim filepath As String
Dim slidestart As String
Dim slideend As String

Set sl = ActivePresentation.Slides(1)
Set tbl = sl.Shapes("Contents").Table

Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
Set slidestart = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 2).Select
Set slideend = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 3).Select

ActivePresentation.Slides.InsertFromFile _
    filepath, 1, slidestart, slideend

End Sub

Solution

  • Ashwin,

    Two hints that aren't obvious to new VBA users:

    1) ALWAYS start every module with Option Explicit. If you go to Tools | Options | Editor tab and put a check next to "Require Variable Declaration", VBA will do this for you automatically. Unless you're on a Mac.

    1a) If you use a different type of casing in your variable names ... FilePath or Filepath rather than filepath ... you can type the names any way you like and the VBA editor will change the casing to what you've declared. If it doesn't, you know you've either mistyped the variable name or not declared it in the first place.

    2) ALWAYS choose Debug | Compile before trying to run your code. Keep compiling until you've found and fixed all compiler errors.

    Here's a modified version of your code. I didn't have time to set up a set of test presentations to try it out on, but I'll give better than even odds that you have that handy. Give it a shot, let us know how it works out.

    Option Explicit
    
    Sub Insert_Slides()
    
    Dim sl As Slide
    Dim tbl As Table
    Dim shp As Shape
    
    Dim filepath As String
    ' These need to be Longs, not Strings
    Dim slidestart As Long
    Dim slideend As Long
    
    Set sl = ActivePresentation.Slides(1)
    Set tbl = sl.Shapes("Contents").Table
    
    ' Use SET to assign objects to object variables, but not to assign values to
    '   string/numeric variables such as you're using.
    
    ' AND never select anything unless there's an absolute need to. It can slow your code down by a factor of 10 and
    '   can prevent it from running in some circumstances.
    
    ' AND by using With/End With you can shorten the code considerably;
    '   easier to read and, at least in theory, runs a bit faster
    
    ' SO ...
    
    'Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
    
    With ActivePresentation.Slides(1).Shapes("Contents").Table
    
        filepath = .Cell(2, 1).Shape.TextFrame.TextRange.Text
    
        ' since the table cells contain text (strings), you should convert them to Longs before assigning
        ' them to a Long variable:
        slidestart = CLng(.Cell(2, 2).Shape.TextFrame.TextRange.Text)
        slideend = CLng(.Cell(2, 3).Shape.TextFrame.TextRange.Text)
    
    End With
    
    ' and you might want to add a test here;
    ' make sure the file exists before trying to access it:
    If Len(Dir$(filepath)) > 0 Then
        ActivePresentation.Slides.InsertFromFile _
           filepath, 1, slidestart, slideend
    Else
        MsgBox "Cannot locate file " & filepath
    End If
    
    End Sub