Freigeben über


Gewusst wie: Anfügen von Erweiterungen durch verwalteten Code an Dokumente

Sie können eine Anpassungsassembly an ein vorhandenes Microsoft Office Word-Dokument oder eine vorhandene Microsoft Office Excel-Arbeitsmappe anfügen.Das Dokument oder die Arbeitsmappe können in jedem Dateiformat vorliegen, das von den Microsoft Office-Projekten und die Entwicklungstools von Visual Studio unterstützt wird.Weitere Informationen finden Sie unter Architektur von Anpassungen auf Dokumentebene.

Betrifft: Die Informationen in diesem Thema betreffen Projekte auf Dokumentebene für die folgenden Anwendungen: Excel 2013 und Excel 2010, Word 2013 und Word 2010. Weitere Informationen finden Sie unter Verfügbare Funktionen nach Office-Anwendung und Projekttyp.

Verwenden Sie zum Anfügen einer Anpassung an ein Word- oder Excel-Dokument die AddCustomization-Methode der ServerDocument-Klasse.Da die ServerDocument-Klasse für die Ausführung auf einem Computer konzipiert ist, auf dem Microsoft Office nicht installiert ist, können Sie diese Methode in Lösungen verwenden, die sich nicht direkt auf Microsoft Office-Entwicklung beziehen, z. B. eine Konsolenanwendung oder Windows Forms-Anwendung).

HinweisHinweis

Falls vom Code erwartete Steuerelemente nicht im angegebenen Dokument enthalten ist, wird die Anpassung nicht geladen.

Link zu Video Eine entsprechende Videodemo finden Sie im Thema zum Anfügen oder Trennen einer VSTO-Assembly an ein bzw. von einem Word-Dokument (möglicherweise in englischer Sprache).

So hängen Sie Erweiterungen durch verwalteten Code an ein Dokument an

  1. In einem Projekt, das nicht Microsoft Office, wie eine Konsolenanwendung oder ein Windows Forms-Projekt erfordert, fügen Sie einen Verweis auf den Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll- und Microsoft.VisualStudio.Tools.Applications.Runtime.dll-Assemblys hinzu.

  2. Fügen Sie am Anfang der Codedatei die folgende Imports-Anweisung bzw. using-Anweisung ein.

    Imports Microsoft.VisualStudio.Tools.Applications
    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  3. Rufen Sie die statische AddCustomization-Methode auf.

    Im folgenden Codebeispiel wird die AddCustomization-Überladung verwendet.Diese Überladung akzeptiert den vollständigen Pfad des Dokuments und einen Uri, der den Speicherort des Bereitstellungsmanifests für die Anpassung angibt, die Sie an das Dokument anfügen möchten.In diesem Beispiel wird davon ausgegangen, dass ein Word-Dokument namens WordDocument1.docx sich auf dem Desktop befindet und dass das Bereitstellungsmanifest in einem Ordner namens Publish enthalten ist, der sich ebenfalls auf dem Desktop befindet.

    Dim documentPath As String = System.Environment.GetFolderPath( _
         Environment.SpecialFolder.Desktop) + "\WordDocument1.docx"
    Dim runtimeVersion As Integer = 0
    
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
    
        ' Make sure that this document does not yet have any Visual Studio Tools 
        ' for Office customizations.
        If runtimeVersion = 0 Then
            Dim deployManifestPath As String = System.Environment.GetFolderPath( _
                Environment.SpecialFolder.Desktop) & "\Publish\WordDocument1.vsto"
            Dim deploymentManifestUri As New Uri(deployManifestPath)
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri)
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.")
        Else
            System.Windows.Forms.MessageBox.Show("The document is already customized.")
        End If
    Catch ex As FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The document could not be customized." & _
            vbLf & ex.Message)
    End Try
    
    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        // Make sure that this document does not yet have any Visual Studio Tools 
        // for Office customizations.
        if (runtimeVersion == 0)
        {
            string deployManifestPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";
    
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
    
  4. Erstellen Sie das Projekt, und führen Sie die Anwendung auf dem Computer aus, auf dem Sie die Anpassung hinzufügen möchten.Der Computer muss die Visual Studio 2010 Tools for Office-Laufzeit installiert haben.

Siehe auch

Aufgaben

Gewusst wie: Entfernen von Erweiterungen durch verwalteten Code aus Dokumenten

Konzepte

Verwalten von Dokumenten auf einem Server mit der ServerDocument-Klasse

Anwendungs- und Bereitstellungsmanifeste in Office-Projektmappen