Share via


Anleitung: Löschen einer Folie aus einer Präsentation

Letzte Änderung: Donnerstag, 14. Oktober 2010

Gilt für: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

Inhalt dieses Artikels
Abrufen eines Presentation-Objekts
Grundlegende Präsentationsdokumentstruktur
Berechnen der Anzahl der Folien
Löschen einer bestimmten Folie
Beispielcode

In diesem Thema wird erklärt, wie Sie das Open XML SDK 2.0 für Microsoft Office zum programmgesteuerten Löschen einer Folie in einer Präsentation verwenden. Ferner wird gezeigt, wie alle Verweise auf die Folie aus ggf. vorhandenen zielgruppenorientierten Präsentationen entfernt werden. Zum Löschen einer bestimmten Folie aus einer Präsentationsdatei müssen Sie zunächst die Anzahl der Folien in der Präsentation kennen. Daher ist der Code in diesem Thema vom Typ "Gewusst wie" in zwei Teile unterteilt. Im ersten Teil wird die Anzahl der Folien berechnet, und im zweiten Teil wird eine Folie an einem bestimmten Index gelöscht.

Hinweis

Zum Löschen einer Folie aus komplexeren Präsentationen, z. B. aus denjenigen, die Gliederungsansichtseinstellungen enthalten, sind ggf. zusätzliche Schritte erforderlich.

Die folgenden Assemblydirektiven sind zum Kompilieren des Codes in diesem Thema erforderlich.

using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Packaging

Abrufen eines Presentation-Objekts

Im Open XML SDK stellt die PresentationDocument-Klasse ein Präsentationsdokumentpaket dar. Zur Verwendung eines Präsentationsdokuments müssen Sie zuerst eine Instanz der PresentationDocument-Klasse erstellen und anschließend mit dieser Instanz arbeiten. Rufen Sie zum Erstellen der Klasseninstanz aus dem Dokument eine der Open-Methodenüberlagerungen auf. Der Code in diesem Thema verwendet die PresentationDocument.Open(String, Boolean)-Methode, die einen Dateipfad als ersten Parameter zum Angeben der zu öffnenden Datei verwendet und einen booleschen Wert als zweiten Parameter, um anzugeben, ob ein Dokument bearbeitet werden kann. Legen Sie diesen zweiten Parameter auf false fest, um die Datei mit Schreibschutz zu öffnen, oder auf true, falls die Datei mit Lese-/Schreibzugriff geöffnet werden soll. Der Code in diesem Thema öffnet die Datei zweimal, einmal zum Berechnen der Anzahl von Folien und einmal zum Löschen einer bestimmten Folie. Zum Zählen der Anzahl von Folien in einer Präsentation wird empfohlen, dass Sie die Datei mit Schreibschutz öffnen, um das versehentliche Schreiben in die Datei zu verhindern. Mit der folgenden using-Anweisung wird die Datei mit Schreibschutz geöffnet. In diesem Codebeispiel ist der presentationFile-Parameter eine Zeichenfolge, die den Pfad der Datei darstellt, aus der Sie das Dokument öffnen möchten.

// Open the presentation as read-only.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
{
    // Insert other code here.
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
' Open the presentation as read-only.
Using presentationDocument As PresentationDocument = PresentationDocument.Open(presentationFile, False)
    ' Insert other code here.
End Using

Öffnen Sie zum Löschen einer Folie aus der Präsentationsdatei die Datei mit Lese-/Schreibzugriff, so wie es in der folgenden using-Anweisung gezeigt wird.

// Open the source document as read/write.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
{
    // Place other code here.
}
' Open the source document as read/write.
Using presentationDocument As PresentationDocument = PresentationDocument.Open(presentationFile, True)
    ' Place other code here.
End Using

Die using-Anweisung ist eine empfohlene Alternative zur herkömmlichen Reihenfolge ".Open, .Save, .Close". Sie stellt sicher, dass die Dispose-Methode (vom Open XML SDK verwendete interne Methode zum Bereinigen von Ressourcen) bei Erreichen der schließenden Klammer automatisch aufgerufen wird. Der auf die using-Anweisung folgende Block richtet einen Bereich für das Objekt ein, das in der using-Anweisung erstellt oder benannt wird, in diesem Fall presentationDocument.

Grundlegende Präsentationsdokumentstruktur

Die grundlegende Dokumentstruktur eines PresentationML-Dokuments besteht aus dem Hauptteil, der die Präsentationsdefinition enthält. In dem folgenden Text aus der ISO/IEC 29500-Spezifikation wird das Gesamtformat eines PresentationML-Pakets erläutert.

Der Hauptteil eines PresentationML-Pakets beginnt mit dem Stammelement der Präsentation. Dieses Element enthält eine Präsentation, die wiederum auf eine Folienliste, eine Folienmasterliste, eine Notizenmasterliste und eine Handzettelmasterliste verweist. Die Folienliste verweist auf alle Folien in der Präsentation. Die Folienmasterliste verweist auf sämtliche in der Präsentation verwendeten Folienmaster. Der Notizenmaster enthält Informationen zur Formatierung der Notizenseiten. Und der Handzettelmaster beschreibt das Aussehen eines Handzettels.

Ein Handzettel ist ein gedruckter Foliensatz, der an eine Zielgruppe zur späteren Bezugnahme verteilt werden kann.

Neben Text und Grafiken kann jede Folie Kommentare und Notizen enthalten, ein Layout aufweisen und Teil mindestens einer zielgruppenorientierten Präsentation sein. Ein Kommentar ist eine Anmerkung der Person, die die Foliengruppe der Präsentation verwaltet. Eine Notiz ist eine Erinnerung oder eine kurze Textstelle, die für den Präsentator oder die Zielgruppe bestimmt ist.

Andere Features, die in einem PresentationML-Dokument enthalten sein können, sind Animationen, Audio, Video und Überblendungen zwischen den Folien.

Ein PresentationML-Dokument wird nicht als ein großer Textkörper in einem einzelnen Teil gespeichert. Die Elemente, mit deren Hilfe bestimmte Funktionsgruppierungen erfolgen, sind stattdessen in mehreren Teilen gespeichert. Beispielsweise sind alle Kommentare in einem Dokument in einem Kommentarteil gespeichert, wobei jede Folie über einen eigenen Teil verfügt.

© ISO/IEC29500: 2008.

Das folgende XML-Codesegment stellt eine Präsentation dar, die zwei Folien mit den IDs 267 und 256 enthält.

<p:presentation xmlns:p="…" … > 
   <p:sldMasterIdLst>
      <p:sldMasterId
         xmlns:rel="http://…/relationships" rel:id="rId1"/>
   </p:sldMasterIdLst>
   <p:notesMasterIdLst>
      <p:notesMasterId
         xmlns:rel="http://…/relationships" rel:id="rId4"/>
   </p:notesMasterIdLst>
   <p:handoutMasterIdLst>
      <p:handoutMasterId
         xmlns:rel="http://…/relationships" rel:id="rId5"/>
   </p:handoutMasterIdLst>
   <p:sldIdLst>
      <p:sldId id="267"
         xmlns:rel="http://…/relationships" rel:id="rId2"/>
      <p:sldId id="256"
         xmlns:rel="http://…/relationships" rel:id="rId3"/>
   </p:sldIdLst>
       <p:sldSz cx="9144000" cy="6858000"/>
   <p:notesSz cx="6858000" cy="9144000"/>
</p:presentation>

Mithilfe des Open XML SDK 2.0 können Sie eine Dokumentstruktur und Inhalte erstellen, indem Sie stark typisierte Klassen verwenden, die PresentationML-Elementen entsprechen. Diese Klassen sind im DocumentFormat.OpenXml.Presentation-Namespace enthalten. Die folgende Tabelle enthält die Namen der Klassen, die den Elementen sld, sldLayout, sldMaster und notesMaster entsprechen.

PresentationML-Element

Open XML SDK 2.0-Klasse

Beschreibung

sld

Slide

Präsentationsfolie. Das SlidePart-Stammelement.

sldLayout

SlideLayout

Das Folienlayout. Das SlideLayoutPart-Stammelement.

sldMaster

SlideMaster

Der Folienmaster. Das SlideMasterPart-Stammelement.

notesMaster

NotesMaster

Notizenmaster (oder Handzettelmaster). Das NotesMasterPart-Stammelement.

Berechnen der Anzahl der Folien

Der Beispielcode besteht aus zwei Überladungen der CountSlides-Methode. Die erste Überladung verwendet einen Parameter vom Typ string, die zweite einen Parameter vom Typ PresentationDocument. In der ersten CountSlides-Methode öffnet der Beispielcode das Präsentationsdokument in der using-Anweisung. Dann übergibt er das PresentationDocument-Objekt an die zweite CountSlides-Methode, die eine ganze Zahl zurückgibt, mit der die Anzahl der Folien in der Präsentation dargestellt wird.

// Pass the presentation to the next CountSlides method
// and return the slide count.
return CountSlides(presentationDocument);
' Pass the presentation to the next CountSlides method
' and return the slide count.
Return CountSlides(presentationDocument)

In der zweiten CountSlides-Methode überprüft der Code, ob das übergebene PresentationDocument-Objekt ungleich null ist. Falls nicht, wird ein PresentationPart-Objekt aus dem PresentationDocument-Objekt abgerufen. Durch Verwendung von SlideParts ruft der Code slideCount ab, und gibt dieses Element zurück.

// Check for a null document object.
if (presentationDocument == null)
{
    throw new ArgumentNullException("presentationDocument");
}

int slidesCount = 0;

// Get the presentation part of document.
PresentationPart presentationPart = presentationDocument.PresentationPart;

// Get the slide count from the SlideParts.
if (presentationPart != null)
{
    slidesCount = presentationPart.SlideParts.Count();
}
// Return the slide count to the previous method.
return slidesCount;
' Check for a null document object.
If presentationDocument Is Nothing Then
    Throw New ArgumentNullException("presentationDocument")
End If

Dim slidesCount As Integer = 0

' Get the presentation part of document.
Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

' Get the slide count from the SlideParts.
If presentationPart IsNot Nothing Then
    slidesCount = presentationPart.SlideParts.Count()
End If
' Return the slide count to the previous method.
Return slidesCount

Löschen einer bestimmten Folie

Der Code zum Löschen einer Folie verwendet zwei Überladungen der DeleteSlide-Methode. Die erste überladene DeleteSlide-Methode verwendet zwei Parameter: eine Zeichenfolge, die den Dateinamen und Pfad der Präsentation darstellt, und eine ganze Zahl, welche die auf 0 basierende Indexposition der zu löschenden Folie darstellt. Die Methode öffnet die Präsentationsdatei mit Lese-/Schreibzugriff, ruft ein PresentationDocument-Objekt ab und übergibt dieses Objekt und den Indexwert an die nächste überladene DeleteSlide-Methode, die den Löschvorgang durchführt.

// Get the presentation object and pass it to the next DeleteSlide method.
public static void DeleteSlide(string presentationFile, int slideIndex)
{
    // Open the source document as read/write.

    using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
    {
      // Pass the source document and the index of the slide to be deleted to the next DeleteSlide method.
      DeleteSlide(presentationDocument, slideIndex);
    }
}  
' Check for a null document object.
If presentationDocument Is Nothing Then
    Throw New ArgumentNullException("presentationDocument")
End If

Dim slidesCount As Integer = 0

' Get the presentation part of document.
Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

' Get the slide count from the SlideParts.
If presentationPart IsNot Nothing Then
    slidesCount = presentationPart.SlideParts.Count()
End If
' Return the slide count to the previous method.
Return slidesCount

Im ersten Abschnitt der zweiten überladenen DeleteSlide-Methode wird die CountSlides-Methode verwendet, um die Anzahl der Folien in der Präsentation abzurufen. Anschließend wird die Liste der Folien-IDs in der Präsentation abgerufen, die angegebene Folie in der Folienliste bestimmt und die Folie aus der Folienliste entfernt.

// Delete the specified slide from the presentation.
public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex)
{
    if (presentationDocument == null)
    {
        throw new ArgumentNullException("presentationDocument");
    }

    // Use the CountSlides sample to get the number of slides in the presentation.
    int slidesCount = CountSlides(presentationDocument);

    if (slideIndex < 0 || slideIndex >= slidesCount)
    {
        throw new ArgumentOutOfRangeException("slideIndex");
    }

    // Get the presentation part from the presentation document. 
    PresentationPart presentationPart = presentationDocument.PresentationPart;

    // Get the presentation from the presentation part.
    Presentation presentation = presentationPart.Presentation;

    // Get the list of slide IDs in the presentation.
    SlideIdList slideIdList = presentation.SlideIdList;

    // Get the slide ID of the specified slide
    SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

    // Get the relationship ID of the slide.
    string slideRelId = slideId.RelationshipId;

    // Remove the slide from the slide list.
    slideIdList.RemoveChild(slideId);
' Delete the specified slide from the presentation.
Public Shared Sub DeleteSlide(ByVal presentationDocument As 
    If presentationDocument Is Nothing Then
        Throw New ArgumentNullException("presentationDocument")
    End If

    ' Use the CountSlides sample to get the number of slides in the presentation.
    Dim slidesCount As Integer = CountSlides(presentationDocument)

    If slideIndex < 0 OrElse slideIndex >= slidesCount Then
        Throw New ArgumentOutOfRangeException("slideIndex")
    End If

    ' Get the presentation part from the presentation document. 
    Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

    ' Get the presentation from the presentation part.
    Dim presentation As Presentation = presentationPart.Presentation

    ' Get the list of slide IDs in the presentation.
    Dim slideIdList As SlideIdList = presentation.SlideIdList

    ' Get the slide ID of the specified slide
    Dim slideId As SlideId = TryCast(slideIdList.ChildElements(slideIndex), SlideId)

    ' Get the relationship ID of the slide.
    Dim slideRelId As String = slideId.RelationshipId

    ' Remove the slide from the slide list.
    slideIdList.RemoveChild(slideId)

Im nächsten Abschnitt der zweiten überladenen DeleteSlide-Methode werden alle Verweise auf die gelöschte Folie aus zielgruppenorientierten Präsentationen gelöscht. Dazu werden die Liste der zielgruppenorientierten Präsentationen und die Liste der Folien in jeder zielgruppenorientierten Präsentation durchlaufen. Dann wird eine verknüpfte Liste der Folienlisteneinträge deklariert und instanziiert, und anhand der Beziehungs-ID dieser Folie wird nach Verweisen auf die gelöschte Folie gesucht. Diese Verweise werden der Liste der Folienlisteneinträge hinzugefügt, und alle diese Verweise werden aus der Folienliste der entsprechenden zielgruppenorientierten Präsentation entfernt.

// Remove references to the slide from all custom shows.
if (presentation.CustomShowList != null)
{
    // Iterate through the list of custom shows.
    foreach (var customShow in presentation.CustomShowList.Elements<CustomShow>())
    {
        if (customShow.SlideList != null)
        {
            // Declare a link list of slide list entries.
            LinkedList<SlideListEntry> slideListEntries = new LinkedList<SlideListEntry>();
            foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
            {
                // Find the slide reference to remove from the custom show.
                if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                {
                    slideListEntries.AddLast(slideListEntry);
                }
            }

            // Remove all references to the slide from the custom show.
            foreach (SlideListEntry slideListEntry in slideListEntries)
            {
                customShow.SlideList.RemoveChild(slideListEntry);
            }
        }
    }
}
' Remove references to the slide from all custom shows.
If presentation.CustomShowList IsNot Nothing Then
    ' Iterate through the list of custom shows.
    For Each customShow In presentation.CustomShowList.Elements(Of CustomShow)()
        If customShow.SlideList IsNot Nothing Then
            ' Declare a link list of slide list entries.
            Dim slideListEntries As New LinkedList(Of SlideListEntry)()
            For Each slideListEntry As SlideListEntry In customShow.SlideList.Elements()
                ' Find the slide reference to remove from the custom show.
                If slideListEntry.Id IsNot Nothing AndAlso slideListEntry.Id = slideRelId Then
                    slideListEntries.AddLast(slideListEntry)
                End If
            Next slideListEntry

            ' Remove all references to the slide from the custom show.
            For Each slideListEntry As SlideListEntry In slideListEntries
                customShow.SlideList.RemoveChild(slideListEntry)
            Next slideListEntry
        End If
    Next customShow
End If

Schließlich wird die geänderte Präsentation vom Code gespeichert, und der Folienteil der gelöschten Folie wird gelöscht.

// Save the modified presentation.
presentation.Save();

// Get the slide part for the specified slide.
SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

// Remove the slide part.
presentationPart.DeletePart(slidePart);
}
' Save the modified presentation.
presentation.Save()

' Get the slide part for the specified slide.
Dim slidePart As SlidePart = TryCast(presentationPart.GetPartById(slideRelId), SlidePart)

' Remove the slide part.
presentationPart.DeletePart(slidePart)
End Sub

Beispielcode

Es folgt der vollständige Beispielcode für die beiden überladenen Methoden CountSlides und DeleteSlide. Verwenden Sie den folgenden Aufruf als ein Beispiel zum Löschen der Folie mit Index 2 in der Präsentationsdatei Myppt6.pptx.

DeleteSlide(@"C:\Users\Public\Documents\Myppt6.pptx", 2);
DeleteSlide("C:\Users\Public\Documents\Myppt6.pptx", 0)

Sie können auch den folgenden Aufruf verwenden, um die Anzahl von Folien in der Präsentation zu zählen.

Console.WriteLine("Number of slides = {0}",
CountSlides(@"C:\Users\Public\Documents\Myppt6.pptx"));
Console.WriteLine("Number of slides = {0}", _
CountSlides("C:\Users\Public\Documents\Myppt6.pptx"))

Es wird empfohlen, dass Sie die Anzahl von Folien zählen, bevor und nachdem Sie die Löschung durchführen.

Es folgt der vollständige Beispielcode in C# und Visual Basic.

// Get the presentation object and pass it to the next CountSlides method.
public static int CountSlides(string presentationFile)
{
    // Open the presentation as read-only.
    using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
    {
        // Pass the presentation to the next CountSlide method
        // and return the slide count.
        return CountSlides(presentationDocument);
    }
}

// Count the slides in the presentation.
public static int CountSlides(PresentationDocument presentationDocument)
{
    // Check for a null document object.
    if (presentationDocument == null)
    {
        throw new ArgumentNullException("presentationDocument");
    }

    int slidesCount = 0;

    // Get the presentation part of document.
    PresentationPart presentationPart = presentationDocument.PresentationPart;

    // Get the slide count from the SlideParts.
    if (presentationPart != null)
    {
         slidesCount = presentationPart.SlideParts.Count();
     }

    // Return the slide count to the previous method.
    return slidesCount;
}
//
// Get the presentation object and pass it to the next DeleteSlide method.
public static void DeleteSlide(string presentationFile, int slideIndex)
{
    // Open the source document as read/write.

    using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
    {
      // Pass the source document and the index of the slide to be deleted to the next DeleteSlide method.
      DeleteSlide(presentationDocument, slideIndex);
    }
}  

// Delete the specified slide from the presentation.
public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex)
{
    if (presentationDocument == null)
    {
        throw new ArgumentNullException("presentationDocument");
    }

    // Use the CountSlides sample to get the number of slides in the presentation.
    int slidesCount = CountSlides(presentationDocument);

    if (slideIndex < 0 || slideIndex >= slidesCount)
    {
        throw new ArgumentOutOfRangeException("slideIndex");
    }

    // Get the presentation part from the presentation document. 
    PresentationPart presentationPart = presentationDocument.PresentationPart;

    // Get the presentation from the presentation part.
    Presentation presentation = presentationPart.Presentation;

    // Get the list of slide IDs in the presentation.
    SlideIdList slideIdList = presentation.SlideIdList;

    // Get the slide ID of the specified slide
    SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

    // Get the relationship ID of the slide.
    string slideRelId = slideId.RelationshipId;

    // Remove the slide from the slide list.
    slideIdList.RemoveChild(slideId);

//
    // Remove references to the slide from all custom shows.
    if (presentation.CustomShowList != null)
    {
        // Iterate through the list of custom shows.
        foreach (var customShow in presentation.CustomShowList.Elements<CustomShow>())
        {
            if (customShow.SlideList != null)
            {
                // Declare a link list of slide list entries.
                LinkedList<SlideListEntry> slideListEntries = new LinkedList<SlideListEntry>();
                foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                {
                    // Find the slide reference to remove from the custom show.
                    if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                    {
                        slideListEntries.AddLast(slideListEntry);
                    }
                }

                // Remove all references to the slide from the custom show.
                foreach (SlideListEntry slideListEntry in slideListEntries)
                {
                    customShow.SlideList.RemoveChild(slideListEntry);
                }
            }
        }
    }

    // Save the modified presentation.
    presentation.Save();

    // Get the slide part for the specified slide.
    SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

    // Remove the slide part.
    presentationPart.DeletePart(slidePart);
}
' Count the number of slides in the presentation.
Public Function CountSlides(ByVal presentationFile As String) As Integer
    ' Open the presentation as read-only.
    Using presentationDocument__1 As PresentationDocument = PresentationDocument.Open(presentationFile, False)
        ' Pass the presentation to the next CountSlides method
        ' and return the slide count.
        Return CountSlides(presentationDocument__1)
    End Using
End Function
' Count the slides in the presentation.
Public Function CountSlides(ByVal presentationDocument As PresentationDocument) As Integer
    ' Check for a null document object.
    If presentationDocument Is Nothing Then
        Throw New ArgumentNullException("presentationDocument")
    End If

    Dim slidesCount As Integer = 0

    ' Get the presentation part of document.
    Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

    If presentationPart IsNot Nothing AndAlso presentationPart.Presentation IsNot Nothing Then
        ' Get the Presentation object from the presentation part.
        Dim presentation As Presentation = presentationPart.Presentation

        ' Verify that the presentation contains slides. 
        If presentation.SlideIdList IsNot Nothing Then

            ' Get the slide count from the slide ID list. 
            slidesCount = presentation.SlideIdList.Elements(Of SlideId)().Count()
        End If
    End If

    ' Return the slide count to the previous method.
    Return slidesCount
End Function
' Delete the specified slide from the presentation.
Public Sub DeleteSlide(ByVal presentationFile As String, ByVal slideIndex As Integer)

    ' Open the source document as read/write.
    Dim presentationDocument As PresentationDocument = presentationDocument.Open(presentationFile, True)

    Using (presentationDocument)

        ' Pass the source document and the index of the slide to be deleted to the next DeleteSlide method.
        DeleteSlide2(presentationDocument, slideIndex)

    End Using

End Sub
' Delete the specified slide in the presentation.
Public Sub DeleteSlide2(ByVal presentationDocument As PresentationDocument, ByVal slideIndex As Integer)
    If (presentationDocument Is Nothing) Then
        Throw New ArgumentNullException("presentationDocument")
    End If

    ' Use the CountSlides code example to get the number of slides in the presentation.
    Dim slidesCount As Integer = CountSlides(presentationDocument)
    If ((slideIndex < 0) OrElse (slideIndex >= slidesCount)) Then
        Throw New ArgumentOutOfRangeException("slideIndex")
    End If

    ' Get the presentation part from the presentation document.
    Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

    ' Get the presentation from the presentation part. 
    Dim presentation As Presentation = presentationPart.Presentation

    ' Get the list of slide IDs in the presentation.
    Dim slideIdList As SlideIdList = presentation.SlideIdList

    ' Get the slide ID of the specified slide.
    Dim slideId As SlideId = CType(slideIdList.ChildElements(slideIndex), SlideId)

    ' Get the relationship ID of the specified slide.
    Dim slideRelId As String = slideId.RelationshipId

    ' Remove the slide from the slide list.
    slideIdList.RemoveChild(slideId)
    ' Remove references to the slide from all custom shows.
    If (Not (presentation.CustomShowList) Is Nothing) Then

        ' Iterate through the list of custom shows.
        For Each customShow As System.Object In presentation.CustomShowList.Elements(Of  _
                               DocumentFormat.OpenXml.Presentation.CustomShow)()

            If (Not (customShow.SlideList) Is Nothing) Then

                ' Declare a linked list.
                Dim slideListEntries As LinkedList(Of SlideListEntry) = New LinkedList(Of SlideListEntry)

                ' Iterate through all the slides in the custom show.
                For Each slideListEntry As SlideListEntry In customShow.SlideList.Elements

                    ' Find the slide reference to be removed from the custom show.
                    If ((Not (slideListEntry.Id) Is Nothing) _
                                AndAlso (slideListEntry.Id = slideRelId)) Then

                        ' Add that slide reference to the end of the linked list.
                        slideListEntries.AddLast(slideListEntry)
                    End If
                Next

                ' Remove references to the slide from the custom show.
                For Each slideListEntry As SlideListEntry In slideListEntries
                    customShow.SlideList.RemoveChild(slideListEntry)
                Next
            End If
        Next
    End If

    ' Save the change to the presentation part.
    presentation.Save()

    ' Get the slide part for the specified slide.
    Dim slidePart As SlidePart = CType(presentationPart.GetPartById(slideRelId), SlidePart)

    ' Remove the slide part.
    presentationPart.DeletePart(slidePart)

End Sub

Siehe auch

Referenz

Class Library Reference