Powerpoint VBA/.Net - Why does Shape.Ungroup differ from ExecuteMso("ObjectsUngroup")?

时间:2017-08-04 13:04:27

标签: .net vb.net vba powerpoint powerpoint-2010

Over quite some time I encountered weird exceptions thrown in a VB.Net Powerpoint VSTO project. After some investigation I was able to find the possible cause:

A different behaviour of Shape Ungroup-Functions in Powerpoint in combination with Undo actions. To reproduce this the following simple environment is necessary:

Start Powerpoint (2010 - I don't have other versions to verify the existence of this behaviour) with one completely empty slide

Put two Rectangles on the slide, group them and save the presentation

Open the saved presentation, select the grouped shape, press ALT-F11 and copy the following 3 Lines in the Immediate window and execute them step by step:

Application.CommandBars.ExecuteMso ("ObjectsUngroup")
Application.CommandBars.ExecuteMso ("Undo")
Debug.Print ActivePresentation.Slides(1).Shapes(1).GroupItems.Count

The result is 2

Now close the presentation unsaved and open it again, press ALT-F11 and copy the following 3 Lines in the Immediate window and execute them step by step:

ActivePresentation.Slides(1).Shapes(1).Ungroup
Application.CommandBars.ExecuteMso ("Undo")
Debug.Print ActivePresentation.Slides(1).Shapes(1).GroupItems.Count

Now the result is acting on a missing/corrupt object reference and a error is raised.

What can be done to use the more flexible Ungroup Function (ExecuteMso is more of a workaround) without raising this error?

UPDATE1: I recognized that this behavior was caused by my own COM-Add-In. But what led to the problem is absolutely unexpected - at least to me :-P

To verify this I stripped down the COM-Add-In to the minimum. I am developing a VB.Net PowerPoint 2010 VSTO-Add-In with Visual Studio 2015

To verify the problem you could simply build a new PowerPoint 2010 VSTO-Add-In Project and in the ThisAddIn.vb Module you paste the following code (there are just 4 new lines [for the Application_SlideSelectionChanged event] compared to the already existing code)

Imports Microsoft.Office.Interop.PowerPoint
Public Class ThisAddIn
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    End Sub
    Private Sub Application_SlideSelectionChanged(SldRange As SlideRange) Handles Application.SlideSelectionChanged
        For Each oShape As PowerPoint.Shape In Globals.ThisAddIn.Application.ActivePresentation.Slides(1).Shapes
        Next
    End Sub
End Class

So it seems the use of For Each with a Shapes collection is the problem. I wasn't aware of that and I didn't expect this either =8-O

So it is nice to see Steve here ;-) who else could be better to explain this :-D

UPDATE2: It is even simpler. Just accessing a member of the Shapes-Collection is sufficient to cause the problem. So even the following code could be used

Imports Microsoft.Office.Interop.PowerPoint
Public Class ThisAddIn
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    End Sub
    Private Sub Application_SlideSelectionChanged(SldRange As SlideRange) Handles Application.SlideSelectionChanged
        MsgBox(Information.TypeName(Globals.ThisAddIn.Application.ActivePresentation.Slides(1).Shapes(1)))
    End Sub
End Class

And while the event is executed no error occurs! I would assume this is a bug?!?

0 个答案:

没有答案