ServerDocument.Save 方法

保存使用 ServerDocument 类对文档所做的任何更改。

命名空间:  Microsoft.VisualStudio.Tools.Applications
程序集:  Microsoft.VisualStudio.Tools.Applications.ServerDocument(在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)

语法

声明
Public Sub Save
public void Save()

异常

异常 条件
DocumentClosedException

ServerDocument 已关闭。

MissingHostItemIdException

CachedDataHostItemId 属性为 nullnull 引用(在 Visual Basic 中为 Nothing) 或为空。

MissingDataIdException

CachedDataItemId 属性为 nullnull 引用(在 Visual Basic 中为 Nothing) 或为空。

MissingTypeException

CachedDataItemDataType 属性为 nullnull 引用(在 Visual Basic 中为 Nothing) 或为空。

备注

Save 方法保存对文档中的缓存数据或部署清单 URL 所做的任何更改。 如果使用磁盘上的文档创建 ServerDocument 对象,则此方法将这些更改保存到磁盘中。 如果使用内存中的文档创建 ServerDocument 对象,则此方法将这些更改保存到内存缓冲区中。

示例

下面的代码示例为指定的 Excel 工作簿创建一个新的 ServerDocument,使用 SerializeDataInstance 方法修改工作表中缓存的字符串的值,然后使用 Save 方法保存更改。

此示例需要:

  • 在 ExcelWorkbook1 命名空间中有 Sheet1 类的 Excel 文档级自定义项,以及 Sheet1 类中名为 CachedString的缓存字符串。

  • 控制台应用程序项目或某些其他非 Office 项目。

  • 对下列程序集的引用:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll(如果项目以 .NET Framework 4 为目标)。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll(如果该项目面向 .NET Framework 3.5)。

  • 位于代码文件顶部的用于 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 命名空间的Imports(对于 Visual Basic)或 using(对于 C#)语句。

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

.NET Framework 安全性

请参见

参考

ServerDocument 类

Microsoft.VisualStudio.Tools.Applications 命名空间