次の方法で共有


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.Applications 名前空間および Microsoft.VisualStudio.Tools.Applications.Runtime 名前空間に対する Imports ステートメント (Visual Basic の場合) または using ステートメント (C# の場合) が必要です。この例では、指定したブックにカスタマイズが含まれ、ExcelWorkbook1 名前空間の Sheet1 クラス、さらに CachedString という名前の Sheet1 クラスにキャッシュされた文字列が含まれていることも前提としています。

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 名前空間