Presentations.Count returns wrong number

Presentations.Count returns wrong number

When programming PowerPoint in VBA, VB, .Net etc., a developer will often use the Presentations.Count method to return the number of items in the Application.Presentations collection.

They may then loop through each presentation in the collection and perform various tasks.

In some cases, the value returned may be one more than the number of presentations the developer thinks they have open. Then when trying to return properties of the extra presentation, a Run-time error ‘-2147188160 (80048240)’ is generated “Presentation (unknown member) : Invalid request. Automation rights are not granted.”

PowerPoint automation rights error -2147188160

Why does this happen?

A little know fact is that the presentations collection is not only a collection of presentations that are open within the PowerPoint user interface but a collection of all PowerPoint presentations that are open across the Windows system.

This includes:

  • Presentations that you opened
  • Presentations that an add-in may have opened but without a window
  • Presentations that Windows Explorer has currently open in Preview mode

The second example can be checked by applying the Presentations(Index).NewWindow method. If the presentation still does not appear and you still get the automation rights error, the chances are that the mysterious presentation is open in a Windows Explorer session as demonstrated in this example where we have clicked on a file in the left hand pane and its slide show starts in the Preview pane on the right.

PowerPoint presentation in Windows Explorer

The good news is that it’s possible to manage this with carefully crafted error handling:

' Returns the number of open presentations, optionally including those where automation rights are not granted e.g. in a Windows Explorer preview
Function PresCount(Optional AllSources As Boolean) As Integer
  ' Defer error handling
  On Error Resume Next
  
  Dim counter As Integer
  Dim TotalPres
  Dim PresName As String
  
  TotalPres = Presentations.Count
  PresCount = TotalPres
  
  If Not AllSources Then
    For counter = 1 To TotalPres
      ' Try to get the presentation name. An "Automation rights are not granted" error ocurs if the presentation is not open in PowerPoint
      PresName = Presentations(counter).Name
      If Not Err = 0 Then PresCount = PresCount - 1: Err.Clear
    Next
  End If
End Function

Need Help with Office Automation?

 

If you have a presentation or Office automation project that you’d like help with, you’ve come to the right place. Our developers know PowerPoint inside out and as you can see above, they know how to program it too. Get in touch for a free discussion about your needs.

 

Submit a Comment

Your email address will not be published. Required fields are marked *