Search code examples
vbapowerpoint

Powerpoint vba - copying table from excel


I have been trying to write a macro that updates a presentation through powerpoint vba using tables from excel.

Here is what I am doing

  • The code first deletes all pictures in the presentation,
  • Then opens the excel workbook, copys a named range and pastes into the correct slide.

This exact code was working fine two days ago and is now saying the object is out of range for copying the range "PL". Any help or tips would be great as this is my first time using powerpoint vba.

valnPath = "G:\valnpath\"
PriorPath = "G:\Priorpath\"

Dim xlApp As Object
Dim xlWorkBook As Object
Dim XL As Excel.Application
Dim PPSlide As PowerPoint.Slide

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlApp.AskToUpdateLinks = False

Set xlWorkBook = xlApp.Workbooks.Open(valnPath & "Presentation Tables 1208.xlsx", True, False)
Set XL = GetObject(, "Excel.Application")
    XL.DisplayAlerts = False
    XL.AskToUpdateLinks = False
    XL.Range("PL").Copy

 ActivePresentation.Slides(3).Select
 Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile

Set XL = GetObject(, "Excel.Application")
    XL.DisplayAlerts = False
    XL.AskToUpdateLinks = False
    XL.Range("AvE").Copy

 ActivePresentation.Slides(5).Select
 Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile

 Set XL = GetObject(, "Excel.Application")
 XL.Quit

Am I doing anything wrong? Please help me with this,

Thank you


Solution

  • Try this:

    valnPath = "G:\valnpath\"
    PriorPath = "G:\Priorpath\"
    
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim PPSlide As PowerPoint.Slide
    
    Set xlApp = CreateObject("Excel.Application")
    With xlApp
       .Visible = True
       .DisplayAlerts = False
       .AskToUpdateLinks = False
    End With
    
    Set xlWorkBook = xlApp.Workbooks.Open(valnPath & _
                           "Presentation Tables 1208.xlsx", True, False)
    
    xlApp.Range("PL").Copy
    ActivePresentation.Slides(3).Select
    Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
    
    xlApp.Range("AvE").Copy
    ActivePresentation.Slides(5).Select
    Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
    
    xlWorkBook.Close False
    xlApp.Quit