ServerDocument.Save (Método)

Guarda todos los cambios realizados en el documento con la clase ServerDocument.

Espacio de nombres:  Microsoft.VisualStudio.Tools.Applications
Ensamblado:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (en Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintaxis

'Declaración
Public Sub Save
public void Save()

Excepciones

Excepción Condición
DocumentClosedException

ServerDocument se ha cerrado.

MissingHostItemIdException

La propiedad Id de CachedDataHostItem es nullreferencia null (Nothing en Visual Basic) o está vacía.

MissingDataIdException

La propiedad Id de CachedDataItem es nullreferencia null (Nothing en Visual Basic) o está vacía.

MissingTypeException

La propiedad DataType de CachedDataItem es nullreferencia null (Nothing en Visual Basic) o está vacía.

Comentarios

El método Save guarda cualquier modificación que se haya realizado en los datos almacenados en caché o en la dirección URL del manifiesto de implementación del documento. Si creó el objeto ServerDocument utilizando un documento almacenado en disco, este método guarda estos cambios en el disco. Si creó el objeto ServerDocument utilizando un documento almacenado en memoria, este método guarda estos cambios en el búfer de memoria.

Ejemplos

En el ejemplo de código siguiente se crea un nuevo ServerDocument para un libro de Excel especificado, se modifica el valor de una cadena almacenada en memoria caché en una hoja de cálculo mediante el método SerializeDataInstance y, a continuación, se guarda el cambio utilizando el método Save.

Este ejemplo necesita:

  • Una personalización de nivel de documento para Excel que tiene una clase Sheet1 en el espacio de nombres ExcelWorkbook1 y una cadena almacenada en memoria caché en la clase Sheet1 denominada CachedString.

  • Un proyecto de aplicación de consola o algún otro proyecto que no es de Office.

  • Referencias a los siguientes ensamblados:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll y Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si el destino del proyecto es .NET Framework 4).

      O bien

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si el proyecto está dirigido a .NET Framework 3.5).

  • Instrucciones Imports (para Visual Basic) o using (para C#) para los espacios de nombres Microsoft.VisualStudio.Tools.Applications y Microsoft.VisualStudio.Tools.Applications.Runtime en la parte superior de su archivo de código.

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();
    }
}

Seguridad de .NET Framework

Vea también

Referencia

ServerDocument Clase

Microsoft.VisualStudio.Tools.Applications (Espacio de nombres)