次の方法で共有


方法 : マネージ コード拡張機能をドキュメントに追加する (2007 システム)

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

Microsoft Office のバージョン

  • 2007 Microsoft Office system

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

Visual Studio Tools for Office カスタマイズを、既存の Microsoft Office Word 2007 文書または Microsoft Office Excel 2007 ブックに追加できます。文書またはブックには、Visual Studio Tools for Office でサポートされている任意のファイル形式を使用できます。詳細については、「ドキュメント レベルのカスタマイズのアーキテクチャ」を参照してください。

Bb772091.alert_note(ja-jp,VS.90).gifメモ :

指定されたドキュメントに、コードで予期されているコントロールが含まれていないと、カスタマイズは読み込みに失敗します。

Visual Studio Tools for Office カスタマイズを Word 文書または Excel ドキュメントに追加するには、ServerDocument クラスの AddCustomization メソッドを使用します。ServerDocument クラスは Microsoft Office がインストールされていないコンピュータで実行することを意図されているため、このメソッドは、Visual Studio Tools for Office プロジェクトをベースとしていないソリューション (コンソール アプリケーションや Windows フォーム アプリケーションなど) で使用できます。

マネージ コード拡張機能をドキュメントに追加するには

  1. Word や Excel を起動しないプロジェクト (コンソール アプリケーションや Windows フォーム アプリケーションなど) を新規に作成します。

  2. 新しいプロジェクトに、次のアセンブリへの参照を追加します。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll

    • Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll

  3. 次の Imports ステートメントまたは using ステートメントをコード ファイルの先頭に追加します。

    Imports Microsoft.VisualStudio.Tools.Applications
    Imports Microsoft.VisualStudio.Tools.Applications.Runtime
    
    using Microsoft.VisualStudio.Tools.Applications;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    
  4. 静的な AddCustomization メソッドを呼び出します。

    次のコード例では、AddCustomization オーバーロードを使用します。このオーバーロードは、ドキュメントの完全パスと、ドキュメントに追加するカスタマイズの配置マニフェストの場所を指定する Uri を使用します。この例では、WordDocument1.docx という Word 文書がデスクトップ上に存在し、配置マニフェストがデスクトップ上の Publish というフォルダにあることを前提としています。

    Dim documentPath As String = System.Environment.GetFolderPath( _
         Environment.SpecialFolder.Desktop) + "\WordDocument1.docx"
    Dim runtimeVersion As Integer = 0
    
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
    
        ' Make sure that this document does not yet have any Visual Studio Tools 
        ' for Office customizations.
        If runtimeVersion = 0 Then
            Dim deployManifestPath As String = System.Environment.GetFolderPath( _
                Environment.SpecialFolder.Desktop) & "\Publish\WordDocument1.vsto"
            Dim deploymentManifestUri As New Uri(deployManifestPath)
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri)
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.")
        Else
            System.Windows.Forms.MessageBox.Show("The document is already customized.")
        End If
    Catch ex As FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The document could not be customized." & _
            vbLf & ex.Message)
    End Try
    
    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        // Make sure that this document does not yet have any Visual Studio Tools 
        // for Office customizations.
        if (runtimeVersion == 0)
        {
            string deployManifestPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";
    
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
    

参照

処理手順

方法 : ServerDocument クラスの両方のバージョンを使用するコードを記述する

方法 : マネージ コード拡張機能をドキュメントから削除する (2007 システム)

方法 : マネージ コード拡張機能をドキュメントから削除する (2003 システム)

方法 : マネージ コード拡張機能をドキュメントに追加する (2003 システム)

概念

ServerDocument クラスによるサーバー上のドキュメントの管理

Office ソリューションにおけるアプリケーション マニフェストと配置マニフェスト