Results 1 to 8 of 8

Thread: Sending Charts to Power Point.

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,098
    Location

    Sending Charts to Power Point.

    The following code sends a chart to a new instance of Powerpoint. How can it be modified to send to an existing presentation?
    Sub ExportChartsToPowerPoint()
        Dim pptApp As Object
        Dim pptPres As Object
        Dim chartObj As ChartObject
        Dim slideIndex As Integer
        ' Create a new instance of PowerPoint application
        Set pptApp = CreateObject("PowerPoint.Application")
        pptApp.Visible = True ' Make PowerPoint visible
        ' Create a new presentation
        Set pptPres = pptApp.Presentations.Add
        slideIndex = 1 ' Initialize slide index
        ' Loop through each chart object in the worksheet
        For Each chartObj In Sheets("Slide 11").ChartObjects
            With chartObj.Chart
                ' Copy the chart
                .ChartArea.Copy
                ' Paste the chart into PowerPoint
                pptPres.Slides.Add slideIndex, 1 ' Add a new slide
                pptPres.Slides(slideIndex).Shapes.PasteSpecial
                ' Adjust the position and size of the pasted chart
                With pptPres.Slides(slideIndex).Shapes(1)
                    .Left = 100 ' Adjust the left position as needed
                    .Top = 100 ' Adjust the top position as needed
                    .Width = 400 ' Adjust the width as needed
                    .Height = 300 ' Adjust the height as needed
                End With
                slideIndex = slideIndex + 1 ' Increment slide index
            End With
        Next chartObj
        ' Clean up
        Set pptPres = Nothing
        Set pptApp = Nothing 
    End Sub


    Actually, on second thoughts, how could it be modified to send a selected chart to a selected powerpoint presentation?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,214
    Location
    There are going to be other methods but the below works for me, it will open a specific pptx file, add a slide, paste the selected/ active chart to that slide.

    You may need to either edit the binding or add a reference to the PowerPoint object library in the VBE.

    Sub ExportChartsToPowerPoint()
        Dim strPresPath As String
        Dim ppApp As PowerPoint.Application
        Dim myPres As PowerPoint.Presentation
        Dim sc As Integer
        
        strPresPath = "C:\Users\jbloggs\Desktop\test.pptx"
        
        Set ppApp = CreateObject("PowerPoint.Application")
        Set myPres = ppApp.Presentations.Open(strPresPath)
        sc = myPres.Slides.Count + 1
        
        With ActiveChart
            .ChartArea.Copy
            myPres.Slides.AddSlide sc, myPres.SlideMaster.CustomLayouts.Item(7)
            myPres.Slides(sc).Select
            ppApp.ActiveWindow.View.Paste
            With myPres.Slides(sc).Shapes(1)
                .Left = 100 ' Adjust the left position as needed
                .Top = 100 ' Adjust the top position as needed
                .Width = 400 ' Adjust the width as needed
                .Height = 300 ' Adjust the height as needed
            End With
        End With
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20140

  3. #3
    Use

    With getobject("G:\OF\example.pptx")
    
    end with
    Avoid 'Select' & 'Activate' & redundant Object Variables in VBA.

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,214
    Location
    @snb

    I have not requested any help.

    If you want to help, why not suggest some code for the OP?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20140

  5. #5
    VBAX Newbie
    Joined
    May 2024
    Posts
    1
    Location
    I had exactly the same question after i tried the solution it worked out for me.
    thx

  6. #6
    Sorry for hacking into this thread, but rather than have a set path to a defined file. Would it be possible to make this code more generalised, so I could maybe open a select file within a folder. Currently I am developing a number of presentations for groups of school children and would like to be able to push a chart into a selection of files as and when I think the need arises?

    The main folder would reside in C: Users/Ian/Maths/ and then Lessons1 to how ever many. If this makes sence.

  7. #7
    Sub M_snb()
       ActiveSheet.ChartObjects(1).Copy
       
       With Application.FileDialog(3)
        .InitialFileName = "G:\powerpoint\*.ppt"
        .AllowMultiSelect = True
        If .Show Then
           For j = 1 To .SelectedItems.Count
            With GetObject(.SelectedItems(j))
              .slides(1).Shapes.PasteSpecial 1, , , , 1
              .Application.Visible = True
              .Close
            End With
           Next
        End If
       End With
    End Sub

  8. #8
    If thats for me snb then thank you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •