次の方法で共有


ControlCollection.AddBuildingBlockGalleryContentControl メソッド (ContentControl, String) (2007 System)

更新 : 2008 年 7 月

新しい BuildingBlockGalleryContentControl をコレクションに追加します。新しいコントロールは、文書内に既に存在するネイティブなコンテンツ コントロールに基づいて作成されます。

名前空間 :  Microsoft.Office.Tools.Word
アセンブリ :  Microsoft.Office.Tools.Word.v9.0 (Microsoft.Office.Tools.Word.v9.0.dll 内)

構文

'宣言
Public Function AddBuildingBlockGalleryContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As BuildingBlockGalleryContentControl
'使用
Dim instance As ControlCollection
Dim contentControl As ContentControl
Dim name As String
Dim returnValue As BuildingBlockGalleryContentControl

returnValue = instance.AddBuildingBlockGalleryContentControl(contentControl, _
    name)
public BuildingBlockGalleryContentControl AddBuildingBlockGalleryContentControl(
    ContentControl contentControl,
    string name
)

パラメータ

戻り値

型 : Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl

文書に追加された BuildingBlockGalleryContentControl

例外

例外 条件
ArgumentNullException

contentControl が nullnull 参照 (Visual Basic では Nothing) です。

または

name が nullnull 参照 (Visual Basic では Nothing) であるか、または長さが 0 である場合。

ControlNameAlreadyExistsException

同じ名前のコントロールが既に ControlCollection に存在する場合。

ArgumentException

contentControl は文書パーツ ギャラリーではありません (contentControl の Type プロパティに値 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlBuildingBlockGallery が設定されていません)。

解説

このメソッドを使用して、文書内に既に存在するネイティブなコンテンツ コントロールに基づく新しい BuildingBlockGalleryContentControl を追加します。これは、実行時に BuildingBlockGalleryContentControl を作成し、この次に文書を開くときに同じコントロールを再作成する場合に便利です。詳細については、「実行時の Office ドキュメントへのコントロールの追加」を参照してください。

次のコード例は、文書内に既に存在するネイティブな文書パーツ ギャラリーごとに新しい BuildingBlockGalleryContentControl を作成します。

このバージョンは、ドキュメント レベルのカスタマイズに使用されます。このコードを使用するには、プロジェクトの ThisDocument クラスにコードを貼り付け、ThisDocument_Startup メソッドから CreateBuildingBlockControlsFromNativeControls メソッドを呼び出します。

Private buildingBlockControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl)

Private Sub CreateBuildingBlockGalleryControlsFromNativeControls()
    If Me.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In Me.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl = _
                Me.Controls.AddBuildingBlockGalleryContentControl(nativeControl, _
                "VSTOBuildingBlockGalleryContentControl" + count.ToString())
            buildingBlockControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl> buildingBlockControls;

private void CreateBuildingBlockControlsFromNativeControls()
{
    if (this.ContentControls.Count <= 0)
        return;

    buildingBlockControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
        {
            count++;
            Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl tempControl =
                this.Controls.AddBuildingBlockGalleryContentControl(nativeControl,
                "VSTOBuildingBlockContentControl" + count.ToString());
            buildingBlockControls.Add(tempControl);
        }
    }
}

このバージョンは、アプリケーション レベルのアドインに使用されます。このコードを使用するには、アドイン プロジェクトの ThisAddIn クラスにコードを貼り付け、ThisAddIn_Startup メソッドから CreateBuildingBlockControlsFromNativeControls メソッドを呼び出します。

Private buildingBlockControls As New System.Collections.Generic.List _
        (Of BuildingBlockGalleryContentControl)

Private Sub CreateBuildingBlockGalleryControlsFromNativeControls()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl = _
                vstoDoc.Controls.AddBuildingBlockGalleryContentControl(nativeControl, _
                "VSTOBuildingBlockGalleryContentControl" + count.ToString())
            buildingBlockControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl> buildingBlockControls;

private void CreateBuildingBlockControlsFromNativeControls()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    if (vstoDoc.ContentControls.Count <= 0)
    {
        System.Windows.Forms.MessageBox.Show("No content controls found in document.");                    
        return;
    }

    buildingBlockControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
        {
            count++;
            Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl tempControl =
                vstoDoc.Controls.AddBuildingBlockGalleryContentControl(nativeControl,
                "VSTOBuildingBlockContentControl" + count.ToString());
            buildingBlockControls.Add(tempControl);
        }
    }
}

次のコード例は、ユーザーが文書に追加するネイティブな文書パーツ ギャラリーごとに新しい BuildingBlockGalleryContentControl を作成します。

このバージョンは、ドキュメント レベルのカスタマイズに使用されます。このコードを使用するには、プロジェクトの ThisDocument クラスにコードを貼り付けます。C# では、さらに ThisDocument_BuildingBlockContentControlAfterAdd イベント ハンドラを ThisDocument クラスの ContentControlAfterAdd イベントに追加する必要があります。

Private Sub ThisDocument_BuildingBlockContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlBuildingBlockGallery Then
        Me.Controls.AddBuildingBlockGalleryContentControl(NewContentControl, _
            "BuildingBlockControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_BuildingBlockContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
    {
        this.Controls.AddBuildingBlockGalleryContentControl(NewContentControl,
            "BuildingBlockControl" + NewContentControl.ID);
    }
}

このバージョンは、アプリケーション レベルのアドインに使用されます。このコードを使用するには、プロジェクトの ThisAddIn クラスにコードを貼り付けます。また、ActiveDocument_BuildingBlockContentControlAfterAdd イベント ハンドラをアクティブ文書の ContentControlAfterAdd イベントに追加する必要があります。

Private Sub ActiveDocument_BuildingBlockContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlBuildingBlockGallery Then
        vstoDoc.Controls.AddBuildingBlockGalleryContentControl(NewContentControl, _
            "BuildingBlockControl" + NewContentControl.ID)
    End If
End Sub
void ActiveDocument_BuildingBlockContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlBuildingBlockGallery)
    {
        vstoDoc.Controls.AddBuildingBlockGalleryContentControl(NewContentControl,
            "BuildingBlockControl" + NewContentControl.ID);
    }
}

アクセス許可

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

参照

参照

ControlCollection クラス

ControlCollection メンバ

AddBuildingBlockGalleryContentControl オーバーロード

Microsoft.Office.Tools.Word 名前空間

その他の技術情報

実行時の Office ドキュメントへのコントロールの追加

ホスト コントロールのヘルパー メソッド

方法 : Word 文書にコンテンツ コントロールを追加する

履歴の変更

日付

履歴

理由

2008 年 7 月

アプリケーション レベルのアドインのコード例のバージョンを追加

SP1 機能変更