ServerDocument 類別 (2007 系統)

更新:2007 年 11 月

提供存取文件中的快取資料和自訂資訊,而該文件屬於 Microsoft Office Excel 2007 或 Microsoft Office Word 2007 文件層級自訂的一部分。

命名空間:  Microsoft.VisualStudio.Tools.Applications
組件:  Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0 (在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 中)

語法

<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public NotInheritable Class ServerDocument _
    Implements IDisposable

Dim instance As ServerDocument
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public sealed class ServerDocument : IDisposable

備註

在未安裝 Excel 或 Word 的電腦上,使用 ServerDocument 類別來管理文件層級自訂的某些方面。

使用 ServerDocument 類別的下列成員執行一般工作:

  • 若要存取及修改伺服器上文件資料快取中的資料,請使用 CachedData 屬性。

  • 若要以程式設計的方式在文件中附加自訂或移除自訂,請使用 AddCustomizationRemoveCustomization 方法。

  • 若要存取或變更與文件關聯之部署資訊清單的 URL,請使用 DeploymentManifestUrl 屬性。

如需詳細資訊,請參閱使用 ServerDocument 類別管理伺服器上的文件

選擇要使用的建構函式

若要使用 ServerDocument 類別存取文件中的快取資料或部署資訊清單 URL,您必須建立 ServerDocument 物件。

有兩組 ServerDocument 建構函式:

  • 一組可用來存取已在記憶體中開啟的文件。

  • 一組可用來存取磁碟上的文件。

存取記憶體中的文件

若要存取已在記憶體中開啟的文件,請使用下列其中一個建構函式:

這些建構函式可接受表示記憶體中之文件的位元組陣列或 Stream。如果您要修改文件中的快取資料或應用程式資訊清單,然後再使用 HTTP 通訊協定將文件資料流處理至目的端,後者相當實用。若要使用這些建構函式,文件必須已經有 Visual Studio Tools for Office 自訂,否則建構函式會擲回 CannotLoadManifestException 例外狀況。

存取磁碟上的文件

若要存取磁碟上的文件,請使用下列其中一個建構函式:

這些建構函式可接受要開啟之文件的完整路徑。根據預設,文件是利用讀取/寫入存取權開啟的。如果您要以唯讀或唯寫存取權開啟文件,請使用具有 FileAccess 參數的建構函式。

了解不同版本的 ServerDocument 類別

Visual Studio Tools for Office 為 Microsoft Office 2003 和 2007 Microsoft Office System 方案提供不同版本的 ServerDocument 類別。這個版本的 ServerDocument 類別只能與利用 Word 2007 和 Excel 2007 文件層級專案範本所建立的方案搭配使用。

若要使用 Word 2003 和 Excel 2003 專案範本所建立的方案,請使用 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空間中的 ServerDocument 類別。

如需詳細資訊,請參閱使用 ServerDocument 類別管理伺服器上的文件。如需示範如何在相同程式碼檔案中使用這兩個版本之 ServerDocument 類別的程式碼範例,請參閱 HOW TO:撰寫使用 ServerDocument 類別兩個版本的程式碼

範例

下列程式碼範例會建立新的 ServerDocument,載入指定的文件,然後顯示附加至文件之 Visual Studio Tools for Office 自訂的部署資訊清單 URL。建立物件之前,程式碼會使用 GetCustomizationVersion 方法,驗證自訂是否使用 Microsoft Visual Studio Tools for the Microsoft Office system (Runtime 3.0 版) 所建立的。它是唯一與這個版本的 ServerDocument 類別相容的執行階段。如需詳細資訊,請參閱使用 ServerDocument 類別管理伺服器上的文件

這個範例要求在您的程式碼檔案頂端要有 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#) 陳述式。

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

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            serverDocument1 = New ServerDocument(documentPath)
            MessageBox.Show("The URL of the deployment manifest is: " & vbLf & _
                serverDocument1.DeploymentManifestUrl.ToString())
        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 CreateServerDocumentFromPath(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath);
            MessageBox.Show("The URL of the deployment manifest is: \n" +
                serverDocument1.DeploymentManifestUrl.ToString());
        }
    }
    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();
    }
}

繼承階層架構

System.Object
  Microsoft.VisualStudio.Tools.Applications.ServerDocument

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

ServerDocument 成員

Microsoft.VisualStudio.Tools.Applications 命名空間

其他資源

使用 ServerDocument 類別管理伺服器上的文件

Office 方案中的應用程式和部署資訊清單

存取伺服器文件中的資料

HOW TO:將資料插入伺服器上的活頁簿中

HOW TO:從伺服器的活頁簿中擷取快取資料

HOW TO:變更伺服器活頁簿中的快取資料