次の方法で共有


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

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

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

構文

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

パラメーター

  • documentPath
    型 : System.String
    カスタマイズをアタッチするドキュメントの完全パスです。
  • assemblyName
    型 : System.String
    カスタマイズ アセンブリの完全パスです。パスは、ローカル ファイル システムまたは UNC 共有上にあることが必要です。HTTP の場所は指定できません。
  • solutionId
    型 : System.Guid
    ソリューションを特定するために Visual Studio Tools for Office Runtime が使用する GUID です。
  • deploymentManifestUrl
    型 : System.Uri
    ソリューションの配置マニフェストの URL。

例外

例外 条件
ArgumentNullException

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

FileNotFoundException

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

DocumentAlreadyCustomizedException

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

DocumentNotCustomizedException

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

UnknownCustomizationFileException

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

解説

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

solutionID パラメーターに渡す GUID は、ドキュメントにアタッチしようとしているソリューションのアプリケーション マニフェストで指定されます。 アプリケーション マニフェストの vstov4:document 要素の solutionId 属性に指定されているのと同じ GUID を渡す必要があります。 詳細については、「Office ソリューションにおけるアプリケーション マニフェストと配置マニフェスト」および「<document> 要素 (Visual Studio での Office 開発)」を参照してください。

カスタマイズを発行場所からアタッチする場合は、assemblyName パラメーターのアセンブリに正しいファイル名を指定してください。 Office ソリューションを発行すると、発行フォルダーにコピーされるアセンブリには .deploy というファイル名拡張子が設定されます。 たとえば、アセンブリ名が WordDocument1.dll である場合、発行フォルダーに保存されるアセンブリのファイル名は WordDocument1.dll.deploy になります。 詳細については、「ClickOnce を使用した Office ソリューションの配置」を参照してください。

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

fileType パラメーターは、Microsoft Office Word および Excel のドキュメント レベルのカスタマイズでサポートされているファイル名拡張子を持つドキュメントを指定する必要があります。 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 を対象とする場合)。

Private Sub AddCustomizationUsingAssemblyPath(ByVal documentPath As String, _
    ByVal assemblyName As String, ByVal solutionID As Guid, 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, assemblyName, solutionID, 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 AddCustomizationUsingAssemblyPath(string documentPath, string assemblyName, 
    Guid solutionID, 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, assemblyName, solutionID, 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 名前空間

その他の技術情報

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

ClickOnce を使用した Office ソリューションの配置