How do i identify an open powerpoint presentation in excel?

zaser

New Member
Joined
Aug 12, 2019
Messages
7
I'm trying to write a macro that automatically uploads a chart to powerpoint but it currently makes a new presentation everytime I try to do it.

I got the macro from the internet and tried to modify it but nothing I try seems to be working

Here's the code(not my code) below


Sub ChartX2P()


Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object


If ActiveChart Is Nothing Then
MsgBox "Hey, please select a chart first."
Exit Sub
End If


If PowerPointApp Is Nothing Then _
Set PowerPointApp = CreateObject(class:="PowerPoint.Application")


On Error GoTo 0


Application.ScreenUpdating = False


Set myPresentation = PowerPointApp.Presentation.Add


Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly


ActiveChart.ChartArea.Copy


mySlide.Shapes.Paste
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)


myShape.Left = 120
myShape.Top = 10


PowerPointApp.Visible = True
PowerPointApp.Activate


Application.CutCopyMode = False


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try the following macro...

Code:
Option Explicit

Sub ChartX2P()


    If ActiveChart Is Nothing Then
        MsgBox "Hey, please select a chart first."
        Exit Sub
    End If
    
    Const POWERPOINT_PRESENTATION_FILENAME As String = "sample.pptx" 'change the file name accordingly


    Dim myPresentation As Object
    On Error Resume Next
    Set myPresentation = GetObject(, "PowerPoint.Application").Presentations(POWERPOINT_PRESENTATION_FILENAME)
    If myPresentation Is Nothing Then
        MsgBox "Open the file named '" & POWERPOINT_PRESENTATION_FILENAME & "', and try again!", vbExclamation
        Exit Sub
    End If
    On Error GoTo 0
    
    Dim mySlide As Object
    Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
    
    'determine where to position the top of the chart
    '25 represents the gap between the bottom of the title placeholder and top of the chart
    Dim topPos As Single
    With mySlide
        topPos = .Shapes(1).Top + .Shapes(1).Height + 25 'change the gap as desired
    End With
    
    Dim myShape As Object
    ActiveChart.ChartArea.Copy
    Set myShape = mySlide.Shapes.Paste(1)
    With myShape
        .Left = 120
        .Top = topPos
    End With
    
    myPresentation.Parent.Parent.Activate

    
    Set myPresentation = Nothing
    Set mySlide = Nothing
    Set myShape = Nothing
    
End Sub

Let me know if you have any questions, or run into any problems.

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,604
Messages
6,173,319
Members
452,510
Latest member
RCan29

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top