ServerDocument.Save 方法 (2007 system)

更新:2007 年 11 月

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

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

语法

声明
Public Sub Save
用法
Dim instance As ServerDocument

instance.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 方法保存更改。

此示例需要在代码文件顶部使用对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll 程序集的引用,并对 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 命名空间使用 Imports(对于 Visual Basic)或 using(对于 C#)语句。此示例还假定指定的工作簿具有一个自定义项,该自定义项在 ExcelWorkbook1 命名空间中有一个 Sheet1 类,在 Sheet1 类中有一个名为 CachedString 的缓存字符串。

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

权限

另请参见

参考

ServerDocument 类

ServerDocument 成员

Microsoft.VisualStudio.Tools.Applications 命名空间