Search code examples
vb.netpowerpointvstooffice-addinspowerpoint-addins

In my PowerPoint VSTO Add-In (VisualBasic) Application.ActivePresentation returns an invalid presentation


I made an PowerPoint VSTO Add-In with VisualBasic in Visual Studio with which I want to be able to copy all slides of a presentation and paste them with their original formatting into my currently active presentation. However it does not quite well work.

If I start up PowerPoint, create a new empty presentation, and click the Add-In it does not work. If I am in debug mode I get an runtime error which tells me "Presentation (unknown member): Object does not exist." at destPPT.Windows.Item(1).Activate() (at the first time I try to use destPPT). When I continue PowerPoint opens the presentation from which I wanted to copy the slides and closes the one I had active.

I found two ways to work around this in PowerPoint. The first one beeing to just close the opened presentation, create a new empty presentation and run the Add-In again. The other one is to modify anything in the empty presentation (like adding some text) and then running the Add-In. Both seem to work fine.

Here is a snippet of the code I used for this

Dim pptApp As Application
Dim destPPT As Presentation
Dim srcPPT As Presentation

pptApp = GetObject([Class]:="PowerPoint.Application")

destPPT = pptApp.ActivePresentation
srcPPT = pptApp.Presentations.Open(srcPath)

srcPPT.Slides.Range().Copy()
destPPT.Windows.Item(1).Activate()
destPPT.Application.CommandBars.ExecuteMso("PasteSourceFormatting")

srcPPT.Close()

I am new to Visual Basic and cant tell whats wrong or what I could do in a different way and couldnt find anything useful anywhere.


Solution

  • After some more debugging and testing around I came to a solution. I created a new empty presentation just like before but then opened the presentation which I wanted to copy the slides from not with the Add-In but with the usual way (File -> Open). PowerPoint now closed the empty presentation again and only opened the new one. I guess that PowerPoint just closes any empty presentation if you havent done anything to it when opening another one. But the problem is not with the actual opening of the presentation but with the opening of a new window for it.

    I now changed the Presentations.Open() WithWindow As MsoTriState parameter to msoFalse so PowerPoint opens the presentation without a new window and it now works as expected.

    My code now looks like this:

    Dim pptApp As Application
    Dim destPPT As Presentation
    Dim srcPPT As Presentation
    
    pptApp = GetObject([Class]:="PowerPoint.Application")
    
    destPPT = pptApp.ActivePresentation
    srcPPT = pptApp.Presentations.Open(srcPath, WithWindow:=MsoTriState.msoFalse)
    
    srcPPT.Slides.Range().Copy()
    'I dont create a new window, therefore the following line is not needed
    'destPPT.Windows.Item(1).Activate()
    destPPT.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
    
    srcPPT.Close()
    

    Edit: It turns out that I dont actually need to activate the window of destPPT anymore because it is the only window created.