次の方法で共有


ControlCollection.AddRichTextContentControl メソッド

定義

オーバーロード

AddRichTextContentControl(String)

ドキュメントの現在の選択範囲に新しい RichTextContentControl を追加します。

AddRichTextContentControl(ContentControl, String)

ドキュメント内のネイティブなコンテンツ コントロールに基づいた新しい RichTextContentControl を追加します。

AddRichTextContentControl(Range, String)

ドキュメント内の指定した範囲に新しい RichTextContentControl を追加します。

AddRichTextContentControl(String)

ドキュメントの現在の選択範囲に新しい RichTextContentControl を追加します。

public:
 Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (string name);
abstract member AddRichTextContentControl : string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (name As String) As RichTextContentControl

パラメーター

name
String

新しいコントロールの名前。

戻り値

ドキュメントに追加された RichTextContentControl

例外

name は、null であるか、または長さがゼロです。

ControlCollection に既に同じ名前のコントロールがあります。

次のコード例では、ドキュメントの先頭に新しい RichTextContentControl を追加します。

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

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;

private void AddRichTextControlAtSelection()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.Select();

    richTextControl1 = this.Controls.AddRichTextContentControl("richTextControl1");
    richTextControl1.PlaceholderText = "Enter your first name";
}
Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddRichTextControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    richTextControl1 = Me.Controls.AddRichTextContentControl("richTextControl1")
    richTextControl1.PlaceholderText = "Enter your first name"
End Sub

このバージョンは、.NET Framework 4 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用です。 このコードを使用するには、プロジェクトの ThisAddIn クラスに貼り付け、 メソッドから メソッドをAddRichTextControlAtSelectionThisAddIn_Startup呼び出します。

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    vstoDoc.Paragraphs[1].Range.Select();

    richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1");
    richTextControl1.PlaceholderText = "Enter your first name";
}
Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    vstoDoc.Paragraphs(1).Range.Select()
    richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1")
    richTextControl1.PlaceholderText = "Enter your first name"
End Sub

注釈

実行時に文書内の現在の選択範囲に新しい RichTextContentControl を追加するには、このメソッドを使用します。 詳細については、「 Adding Controls to Office Documents at Run Time」を参照してください。

適用対象

AddRichTextContentControl(ContentControl, String)

ドキュメント内のネイティブなコンテンツ コントロールに基づいた新しい RichTextContentControl を追加します。

public:
 Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddRichTextContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (contentControl As ContentControl, name As String) As RichTextContentControl

パラメーター

contentControl
ContentControl

新しいコントロールのベースとなる ContentControl

name
String

新しいコントロールの名前。

戻り値

ドキュメントに追加された RichTextContentControl

例外

contentControlnull.-or- name または の null 長さが 0 です。

ControlCollection に既に同じ名前のコントロールがあります。

contentControlは文書パーツ ギャラリーではありません (つまり、Microsoft.Office.Interop.Word。の contentControl ContentControl.Type プロパティには、Microsoft.Office.Interop.Word の値がありません。WdContentControlType.wdContentControlRichText)。

次のコード例では、ドキュメント内のすべてのネイティブ リッチ テキスト コントロールに対して新しい RichTextContentControl を作成します。

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

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type ==
            Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
        {
            count++;
            Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                this.Controls.AddRichTextContentControl(nativeControl,
                "VSTORichTextControl" + count.ToString());
            richTextControls.Add(tempControl);
        }
    }
}
Private richTextControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.RichTextContentControl)

Private Sub CreateRichTextControlsFromNativeControls()
    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.wdContentControlRichText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                Me.Controls.AddRichTextContentControl(nativeControl, _
                "VSTORichTextContentControl" + count.ToString())
            richTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

このバージョンは、.NET Framework 4 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用です。 このコードを使用するには、プロジェクトの ThisAddIn クラスに貼り付け、 メソッドから メソッドをCreateRichTextControlsFromNativeControlsThisAddIn_Startup呼び出します。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;

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

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

    richTextControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl>();
    int count = 0;
    
    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type ==
            Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
        {
            count++;
            Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                vstoDoc.Controls.AddRichTextContentControl(nativeControl,
                "VSTORichTextControl" + count.ToString());
            richTextControls.Add(tempControl);
        }
    }
}
Private richTextControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.RichTextContentControl)

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    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.wdContentControlRichText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                vstoDoc.Controls.AddRichTextContentControl(nativeControl, _
                "VSTORichTextContentControl" + count.ToString())
            richTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

次のコード例では、ユーザーがドキュメントに追加するすべてのネイティブ リッチ テキスト コントロールに対して新しい RichTextContentControl を作成します。

このバージョンは、ドキュメント レベルのカスタマイズ用です。 このコードを使用するには、プロジェクトの クラスに ThisDocument 貼り付けます。 C# の場合は、イベント ハンドラーを ThisDocument_RichTextContentControlAfterAdd クラスの ThisDocument イベントにContentControlAfterAddアタッチする必要もあります。

void ThisDocument_RichTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
    {
        this.Controls.AddRichTextContentControl(NewContentControl,
            "RichTextControl" + NewContentControl.ID);
    }
}
Private Sub ThisDocument_RichTextContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlRichText Then
        Me.Controls.AddRichTextContentControl(NewContentControl, _
            "RichTextControl" + NewContentControl.ID)
    End If
End Sub

このバージョンは、.NET Framework 4 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用です。 このコードを使用するには、プロジェクトの クラスに ThisAddIn 貼り付けます。 また、作業中の文書の ActiveDocument_RichTextContentControlAfterAdd イベントにイベント ハンドラーを ContentControlAfterAdd アタッチする必要があります。

void ActiveDocument_RichTextContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
    {
        vstoDoc.Controls.AddRichTextContentControl(NewContentControl,
            "RichTextControl" + NewContentControl.ID);
    }
}
Private Sub ActiveDocument_RichTextContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlRichText Then
        vstoDoc.Controls.AddRichTextContentControl(NewContentControl, _
            "RichTextControl" + NewContentControl.ID)
    End If
End Sub

注釈

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

適用対象

AddRichTextContentControl(Range, String)

ドキュメント内の指定した範囲に新しい RichTextContentControl を追加します。

public:
 Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddRichTextContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (range As Range, name As String) As RichTextContentControl

パラメーター

range
Range

新しいコントロールの境界を指定する Range

name
String

新しいコントロールの名前。

戻り値

ドキュメントに追加された RichTextContentControl

例外

name は、null であるか、または長さがゼロです。

ControlCollection に既に同じ名前のコントロールがあります。

次のコード例では、ドキュメントの先頭に新しい RichTextContentControl を追加します。

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

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;

private void AddRichTextControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();

    richTextControl2 = this.Controls.AddRichTextContentControl(this.Paragraphs[1].Range,
        "richTextControl2");
    richTextControl2.PlaceholderText = "Enter your first name";
}
Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddRichTextControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    richTextControl2 = Me.Controls.AddRichTextContentControl(Me.Paragraphs(1).Range, _
        "richTextControl2")
    richTextControl2.PlaceholderText = "Enter your first name"
End Sub

このバージョンは、.NET Framework 4 または .NET Framework 4.5 を対象とするアプリケーション レベルのアドイン用です。 このコードを使用するには、プロジェクトの ThisAddIn クラスに貼り付け、 メソッドから メソッドをAddRichTextControlAtRangeThisAddIn_Startup呼び出します。

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();

    richTextControl2 = vstoDoc.Controls.AddRichTextContentControl(vstoDoc.Paragraphs[1].Range,
        "richTextControl2");
    richTextControl2.PlaceholderText = "Enter your first name";
}
Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    richTextControl2 = vstoDoc.Controls.AddRichTextContentControl( _
        vstoDoc.Paragraphs(1).Range, _
        "richTextControl2")
    richTextControl2.PlaceholderText = "Enter your first name"
End Sub

注釈

実行時にドキュメント内の指定した範囲に新しい RichTextContentControl を追加するには、このメソッドを使用します。 詳細については、「 Adding Controls to Office Documents at Run Time」を参照してください。

適用対象