Share via


ServerDocument.Save, méthode

Enregistre toutes les modifications apportées au document en utilisant la classe ServerDocument.

Espace de noms :  Microsoft.VisualStudio.Tools.Applications
Assembly :  Microsoft.VisualStudio.Tools.Applications.ServerDocument (dans Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Syntaxe

'Déclaration
Public Sub Save
public void Save()

Exceptions

Exception Condition
DocumentClosedException

ServerDocument a été fermé.

MissingHostItemIdException

La propriété Id d'un CachedDataHostItem est nullune référence null (Nothing en Visual Basic) ou est vide.

MissingDataIdException

La propriété Id d'un CachedDataItem est nullune référence null (Nothing en Visual Basic) ou est vide.

MissingTypeException

La propriété DataType d'un CachedDataItem est nullune référence null (Nothing en Visual Basic) ou est vide.

Notes

La méthode Save enregistre toutes les modifications que vous avez apportées aux données en mémoire cache ou à l'URL du manifeste de déploiement dans le document.Si vous avez créé l'objet ServerDocument en utilisant un document sur disque, cette méthode enregistre ces modifications sur disque.Si vous avez créé l'objet ServerDocument en utilisant un document en mémoire, cette méthode enregistre ces modifications dans la mémoire tampon.

Exemples

L'exemple de code suivant crée un ServerDocument pour un classeur Excel spécifié, modifie la valeur d'une chaîne mise en cache dans une feuille de calcul à l'aide de la méthode SerializeDataInstance, puis enregistre la modification à l'aide de la méthode Save.

Cet exemple nécessite :

  • Une personnalisation au niveau du document pour Excel qui a une classe Sheet1 dans l'espace de noms ExcelWorkbook1, et une chaîne mise en cache dans la classe Sheet1 nommée CachedString.

  • Projet d'application console ou un autre projet non-Office.

  • Références aux assemblys suivants :

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si le projet cible .NET Framework 4 ou .NET Framework 4.5).

      ou

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll et Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si le projet cible le .NET Framework 3.5).

  • Instructions Imports (pour Visual Basic) ou using pour C#) pour les espaces de noms Microsoft.VisualStudio.Tools.Applications et Microsoft.VisualStudio.Tools.Applications.Runtime au haut de votre fichier de code.

Private Sub ModifyCachedString(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion <> 3 Then
            MessageBox.Show("This document does not have a Visual Studio Tools for Office " & _
                "customization, or it has a customization that was created with a version of " & _
                "the runtime that is incompatible with this version of the ServerDocument class.")
            Return
        End If

        If ServerDocument.IsCacheEnabled(documentPath) Then
            serverDocument1 = New ServerDocument(documentPath)
            Dim hostItem1 As CachedDataHostItem = _
                serverDocument1.CachedData.HostItems("ExcelWorkbook1.Sheet1")
            Dim dataItem1 As CachedDataItem = hostItem1.CachedData("CachedString")

            If dataItem1 IsNot Nothing AndAlso _
                Type.GetType(dataItem1.DataType).Equals(GetType(String)) Then

                dataItem1.SerializeDataInstance("This is the new cached string value.")
                serverDocument1.Save()
            End If
        Else
            MessageBox.Show("The specified document does not have cached data.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
    End Try
End Sub
private void ModifyCachedString(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
        {
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");
            return;
        }

        if (ServerDocument.IsCacheEnabled(documentPath))
        {
            serverDocument1 = new ServerDocument(documentPath);
            CachedDataHostItem hostItem1 = 
                serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
            CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

            if (dataItem1 != null &&
                Type.GetType(dataItem1.DataType) == typeof(string))
            {
                dataItem1.SerializeDataInstance("This is the new cached string value.");
                serverDocument1.Save();
            }
        }
        else
        {
            MessageBox.Show("The specified document does not have cached data.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
    }
}

Sécurité .NET Framework

Voir aussi

Référence

ServerDocument Classe

Microsoft.VisualStudio.Tools.Applications, espace de noms