简体   繁体   中英

VBA Copy range from excel to powerpoint

I am trying to copy a specific range from excel and past it in pp as a picture. I have pieced together the following code from various online sources and continue to get a run time 91 error ( Object Variable or With block variable not set) when running PowerPointApp.WindowState = 2.

How can I fix this error, and avoid it in future?

first i successfully run

    Private Sub OpenPowerpoint()

        Dim PPT As PowerPoint.Application
        Set PPT = New PowerPoint.Application

        PPT.Visible = True
        PPT.Presentations.Open Filename:="C:\Users\aofarrell\Desktop\CYB\Weekly Pack Update - Template.pptx"
        PPT.ActivePresentation.Slides(2).Select

    End Sub

Then I attempt to run

 Private Sub CopyToPowerPoint()

            Dim rng As Range
            Dim PowerPointApp As Object
            Dim mySlide As Object
            Dim myShape As Object


        'Copy Range from Excel

            Set rng = ThisWorkbook.Sheets("Triggers").Range("B6:Z33")

        'Copy Excel Range
            rng.Copy

        'Paste to PowerPoint and position
            PowerPointApp.WindowState = 2 'ERROR OCCURS HERE
            mySlide.Shapes.PasteSpecial DataType:=0
            Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

        'Set position:
            myShape.Left = 20
            myShape.Top = 70
            myShape.Width = 675
            myShape.Height = 400

        'Clear The Clipboard
            Application.CutCopyMode = False
            Application.Wait (Now + TimeValue("00:00:01"))

        End Sub

Hmm... Well, first you need to define what type of Object your PowerPointApp is. And what specific object your mySlide is. Remember also that Local variables are destroyed at the end of the Sub/Function, so you may want some Module level variables/objects instead:

Option Explicit

Private PPT As PowerPoint.Application
Private PPT_pres As PowerPoint.Presentation

Private Sub OpenPowerpoint()
    Set PPT = New PowerPoint.Application

    PPT.Visible = True
    Set PPT_pres = PPT.Presentations.Open(FileName:="C:\Users\aofarrell\Desktop\CYB\Weekly Pack Update - Template.pptx")
    PPT_pres.Slides(2).Select
End Sub

Private Sub CopyToPowerPoint()
    If PPT Is Nothing Then Exit Sub
    If PPT_pres Is Nothing Then Exit Sub

    Dim rng As Range
    Dim mySlide As Object
    Dim myShape As Object

    Set mySlide = PPT_pres.Slides(2)

    'Copy Range from Excel

    Set rng = ThisWorkbook.Sheets("Triggers").Range("B6:Z33")

    'Copy Excel Range
    rng.Copy

    'Paste to PowerPoint and position
    PPT.WindowState = 2
    mySlide.Shapes.PasteSpecial DataType:=0
    Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

    'Set position:
    myShape.Left = 20
    myShape.Top = 70
    myShape.Width = 675
    myShape.Height = 400

    'Clear The Clipboard
    Application.CutCopyMode = False
    Application.Wait (Now + TimeValue("00:00:01"))

End Sub

(Also: if I was copying as an Image from Excel to PowerPoint, I would usually use Range.CopyPicture xlPrinter rather than Shapes.PasteSpecial which changes the size of the image based on your screen resolution)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM