次の方法で共有


ServerDocument.AddCustomization メソッド (String, Uri)

指定されたアセンブリ名と配置マニフェストを使用して、特定のドキュメントにカスタマイズをアタッチします。

名前空間:  Microsoft.VisualStudio.Tools.Applications
アセンブリ:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 内)

構文

'宣言
Public Shared Sub AddCustomization ( _
    documentPath As String, _
    deploymentManifestUrl As Uri _
)
public static void AddCustomization(
    string documentPath,
    Uri deploymentManifestUrl
)

パラメーター

  • documentPath
    型 : System.String
    カスタマイズをアタッチするドキュメントの完全パスです。
  • deploymentManifestUrl
    型 : System.Uri
    ソリューションの配置マニフェストの URL。

例外

例外 条件
ArgumentNullException

documentPath または deploymentManifestUrl が、nullnull 参照 (Visual Basic では Nothing) または空です。

ArgumentException

deploymentManifestUrl は、絶対 URL を指定しません。

FileNotFoundException

documentPath または deploymentManifestUrl が、存在しないファイルを参照しています。

DocumentAlreadyCustomizedException

documentPath で指定されたドキュメントには、既にカスタマイズが含まれています。

InvalidManifestException

deploymentManifestUrl で指定された配置マニフェストは、有効な配置マニフェストではありません。

DocumentNotCustomizedException

documentPath で指定されたドキュメントは、破損しているか、アクセス許可が制限されています。

UnknownCustomizationFileException

documentPath で指定されたドキュメントで、Visual Studio Tools for Office Runtime でサポートされていないファイル名の拡張子が使用されています。

解説

AddCustomization メソッドは、ドキュメントに対して _AssemblyName および _AssemblyLocation カスタム ドキュメント プロパティを追加することで、指定されたカスタマイズをドキュメントに関連付けます。 これらのプロパティは、ドキュメントにカスタマイズが含まれていることを示し、配置マニフェストの場所を指定します。 このメソッドが正常に呼び出された場合、それ以降にユーザーが特定のドキュメントを開いたときに、ランタイムにより Office ソリューションのインストールが試行されます。 カスタム ドキュメント プロパティの詳細については、「カスタム ドキュメント プロパティの概要」を参照してください。

指定されたドキュメントにカスタマイズで予期されているコントロールが含まれていない場合、AddCustomization メソッドは正常に実行されますが、ユーザーがドキュメントを開くときアセンブリは読み込まれません。

fileType パラメーターは、ドキュメント レベルのカスタマイズでサポートされているファイル名拡張子を持つドキュメントを指定する必要があります。 Word XML 文書 (*xml) または Word 2003 XML 文書 (*xml) ファイル形式で保存される文書にカスタマイズをアタッチすることはできません。 サポートされるファイルの種類の詳細については、「ドキュメント レベルのカスタマイズのアーキテクチャ」を参照してください。

次のコード例は、AddCustomization メソッドを使用して、指定されたドキュメントにカスタマイズをアタッチします。

この例には、次の項目が必要です。

  • コンソール アプリケーション プロジェクトまたはその他の Office 以外のプロジェクト。

  • 次のアセンブリへの参照。

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll および Microsoft.VisualStudio.Tools.Applications.Runtime.dll (プロジェクトが .NET Framework 4 または .NET Framework 4.5 を対象とする場合)。

      または

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll および Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (プロジェクトが .NET Framework 3.5 を対象とする場合)。

  • コード ファイルの先頭に、Microsoft.VisualStudio.Tools.Applications 名前空間および Microsoft.VisualStudio.Tools.Applications.Runtime 名前空間に対する Imports ステートメント (Visual Basic の場合) または using ステートメント (C# の場合) を追加します。

Private Sub AddCustomizationUsingDocumentPath(ByVal documentPath As String, _
    ByVal deployManifestPath As String)
    Dim runtimeVersion As Integer = 0

    Try
        ' Make sure that this document does not yet have any Visual Studio Tools 
        ' for Office customizations.
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 0 Then
            Dim deploymentManifestUri As New Uri(deployManifestPath)
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri)
            MessageBox.Show("The document was successfully customized.")
        Else
            System.Windows.Forms.MessageBox.Show("The document is already customized.")
        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.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The document could not be customized." & _
        vbLf & ex.Message)
    End Try
End Sub
private void AddCustomizationUsingDocumentPath(string documentPath, string deployManifestPath)
{
    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)
        {
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    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.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
}

.NET Framework セキュリティ

  • 直前の呼び出し元に対する完全な信頼。このメンバーは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

関連項目

ServerDocument クラス

AddCustomization オーバーロード

Microsoft.VisualStudio.Tools.Applications 名前空間