Excel Macro That Checks If PowerPoint Presentation Is Open

Draperies

Board Regular
Joined
Jun 29, 2009
Messages
79
I am running a macro from excel that copies tables and charts into a PowerPoint template. If the template file is already open I'd like to set it as the presentation I'm going to work with and if it is not open I want to open it. I have tried to adapt a number of approaches that I found from other places but I could not get anything to work. Here are a few of the thing I tried:

Code:
Dim pptPrs As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Dim opnFlg As Boolean

    opnFlg = False
    For Each pptPrs In PowerPoint.Presentations
        If pptPrs.Name = "template.pptx" Then
            opnFlg = True
            Set pptApp = GetObject(, objPrs)
            pptApp.Visible = True
        End If
    Next
    If opnFlg = False Then
        Set pptApp = New PowerPoint.Application
        pptApp.Visible = True
        pptApp.Presentations.Open Filename:="C:\test\template.pptx"
    End If

Here is a function someone wrote and I modified somewhat that only does the checking to see if the presentation is open... however this also I could not get to work:

Code:
Function IsPresentationOpen(strPresName As String) As Boolean
Dim oPresObject As PowerPoint.Presentation
Dim boolIsFullPath As Boolean

   ' Check to see whether the full path was passed.
   If (InStr(1, strPresName, ":\")) = 0 Then
      boolIsFullPath = False
   Else
      boolIsFullPath = True
   End If

   ' Loop through the open presentations.
   Set oPresObject = CreateObject("PowerPoint.Presentation")
   For Each oPresObject In PowerPoint.Presentations
      If boolIsFullPath = True Then
         ' Check for a match.
         If (StrComp(oPresObject.FullName, strPresName, vbTextCompare) = 0) Then
            IsPresentationOpen = True
            Exit Function
         End If
      Else
         ' Check for a match.
         If (StrComp(oPresObject.Name, strPresName, vbTextCompare) = 0) Then
            IsPresentationOpen = True
            Exit Function
         End If
      End If
   Next oPresObject

   ' No match found.
   IsPresentationOpen = False

End Function

Any help is much appreciated. Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Just an update regarding some my earlier question. I was finally able to get some of what I initially requested to work with the following code:

Code:
Dim pptApp As PowerPoint.Application
Dim pptPrs As PowerPoint.Presentation

    On Error Resume Next
    Set pptApp = GetObject(, "Powerpoint.Application")
    If pptApp Is Nothing Then
        Set pptApp = CreateObject("PowerPoint.Application")
        pptApp.Visible = True
        pptApp.Presentations.Open Filename:="C:\test\template.pptx"
        Set pptPres = pptApp.ActivePresentation
    Else
        pptApp.Visible = True
        Set pptPres = pptApp.ActivePresentation
    End If

This however does not check if a specific PowerPoint file is open and so only works if the already opened PowerPoint file is in fact the template file. Any suggestions in handling that aspect would be appreciated!
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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