Excel Macro - Как вставить Excel Chart в уже открытый PPT - PullRequest
0 голосов
/ 28 сентября 2018

У меня уже есть открытый ppt, и я пытаюсь вставить в него таблицы Excel из Worksheet = PivotChart.Код ниже не работает.Это было предоставлено мне старшим, который не может решить.Я не могу выяснить проблему.

'---------------------- Создание PPT --------------------- '

   'First we declare the variables we will be using
    Dim newPowerPoint As PowerPoint.Application
    Dim activeSlide As PowerPoint.Slide
    Dim cht As Excel.ChartObject
    Dim XLApp As Excel.Application

 'Look for existing instance
    On Error Resume Next
    Set newPowerPoint = GetObject(, "PowerPoint.Application")
    On Error GoTo 0

'Let's create a new PowerPoint
    If newPowerPoint Is Nothing Then
        Set newPowerPoint = New PowerPoint.Application
    End If
'Make a presentation in PowerPoint
    If newPowerPoint.Presentations.Count = 0 Then
        newPowerPoint.Presentations.Add
    End If

'Title Slide
    newPowerPoint.Visible = True
    newPowerPoint.ActivePresentation.Slides.Add 
 newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
        newPowerPoint.ActiveWindow.View.GotoSlide 
 newPowerPoint.ActivePresentation.Slides.Count
        Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
        activeSlide.Shapes(1).TextFrame.TextRange.Text = "AUTOMATED TICKET ANALYSIS"

            Set XLApp = GetObject(, "Excel.Application")
            XLApp.Range("Y66:Z77").Select
            XLApp.Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
            activeSlide.Shapes.Paste.Select

'Adjust the positioning of the Chart on Powerpoint Slide
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 60
        newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 200

        activeSlide.Shapes(3).Width = 500
        activeSlide.Shapes(2).Width = 300
        activeSlide.Shapes(2).Left = 600

 XLApp.Range("AR100").Select
        XLApp.Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        activeSlide.Shapes.Paste.Select
' Adjust the positioning of text box
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 580
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 200

'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
    For Each cht In ActiveSheet.ChartObjects

    'Add a new slide where we will paste the chart
        newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
        newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
        Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

    'Copy the chart and paste it into the PowerPoint as a Metafile Picture
        cht.Select
        ActiveChart.ChartArea.Copy

activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select


'Set the title of the slide the same as the title of the chart
        activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text

 'Adjust the positioning of the Chart on Powerpoint Slide
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 80
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 150

        activeSlide.Shapes(3).Width = 500
        activeSlide.Shapes(2).Width = 300
        activeSlide.Shapes(2).Left = 600

   Next
'HeatMap getting pasted to new slide
            newPowerPoint.ActivePresentation.Slides.Add 
newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
            newPowerPoint.ActiveWindow.View.GotoSlide 
newPowerPoint.ActivePresentation.Slides.Count
            Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

Set XLApp = GetObject(, "Excel.Application")
XLApp.Range("M65:U90").Select
XLApp.Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
activeSlide.Shapes.Paste.Select

'Adjust the positioning of the Chart on Powerpoint Slide
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 380
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 90

        activeSlide.Shapes(3).Width = 500
        activeSlide.Shapes(2).Width = 300
        activeSlide.Shapes(2).Left = 600

  activeSlide.Shapes(1).TextFrame.TextRange.Text = "HEAT MAP"

   ' End Slide
newPowerPoint.ActivePresentation.Slides.Add 
newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
            newPowerPoint.ActiveWindow.View.GotoSlide 
newPowerPoint.ActivePresentation.Slides.Count
            Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
            activeSlide.Shapes(1).TextFrame.TextRange.Text = "Thank You!"


'AppActivate ("Microsoft PowerPoint")'
    Set activeSlide = Nothing
    Set newPowerPoint = Nothing
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...