次の方法で共有


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

更新 : 2008 年 7 月

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

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

構文

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

returnValue = instance.AddDropDownListContentControl(contentControl, _
    name)
public DropDownListContentControl AddDropDownListContentControl(
    ContentControl contentControl,
    string name
)

パラメータ

戻り値

型 : Microsoft.Office.Tools.Word.DropDownListContentControl

文書に追加された DropDownListContentControl

例外

例外 条件
ArgumentNullException

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

または

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

ControlNameAlreadyExistsException

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

ArgumentException

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

解説

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

次のコード例は、文書内にあるネイティブなドロップダウン リストごとに新しい DropDownListContentControl を作成します。

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

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

Private Sub CreateDropDownListControlsFromNativeControls()
    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.wdContentControlDropdownList Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.DropDownListContentControl = _
                Me.Controls.AddDropDownListContentControl(nativeControl, _
                "VSTODropDownListContentControl" + count.ToString())
            dropDownListControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.DropDownListContentControl> dropDownControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlDropdownList)
        {
            count++;
            Microsoft.Office.Tools.Word.DropDownListContentControl tempControl =
                this.Controls.AddDropDownListContentControl(nativeControl,
                "VSTODropDownListContentControl" + count.ToString());
            dropDownControls.Add(tempControl);
        }
    }
}

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

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

Private Sub CreateDropDownListControlsFromNativeControls()
    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.wdContentControlDropdownList Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.DropDownListContentControl = _
                vstoDoc.Controls.AddDropDownListContentControl(nativeControl, _
                "VSTODropDownListContentControl" + count.ToString())
            dropDownListControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.DropDownListContentControl> dropDownControls;

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

    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    if (vstoDoc.ContentControls.Count <= 0)
        return;

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlDropdownList)
        {
            count++;
            Microsoft.Office.Tools.Word.DropDownListContentControl tempControl =
                vstoDoc.Controls.AddDropDownListContentControl(nativeControl,
                "VSTODropDownListContentControl" + count.ToString());
            dropDownControls.Add(tempControl);
        }
    }
}

次のコード例は、ユーザーが文書に追加するネイティブなドロップダウン リストごとに新しい DropDownListContentControl を作成します。

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

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

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlDropdownList Then
        Me.Controls.AddDropDownListContentControl(NewContentControl, _
            "DropDownListControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_DropDownListContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlDropdownList)
    {
        this.Controls.AddDropDownListContentControl(NewContentControl,
            "DropDownListControl" + NewContentControl.ID);
    }
}

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

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

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

アクセス許可

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

参照

参照

ControlCollection クラス

ControlCollection メンバ

AddDropDownListContentControl オーバーロード

Microsoft.Office.Tools.Word 名前空間

その他の技術情報

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

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

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

履歴の変更

日付

履歴

理由

2008 年 7 月

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

SP1 機能変更