Share via


ControlCollection.AddRichTextContentControl 方法 (ContentControl, String)

加入新的 RichTextContentControl,此控制項是以文件中的原生內容控制項為基礎。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Function AddRichTextContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As RichTextContentControl
RichTextContentControl AddRichTextContentControl(
    ContentControl contentControl,
    string name
)

參數

傳回值

型別:Microsoft.Office.Tools.Word.RichTextContentControl
加入至文件中的 RichTextContentControl

例外狀況

例外狀況 條件
ArgumentNullException

contentControl 為 nullnull 參考 (即 Visual Basic 中的 Nothing)。

-或-

name 是 nullnull 參考 (即 Visual Basic 中的 Nothing),或長度為 0。

ControlNameAlreadyExistsException

ControlCollection 中已有相同名稱的控制項。

ArgumentException

contentControl 不是建置組塊陳列庫 (即 contentControl 的 Microsoft.Office.Interop.Word.ContentControl.Type 屬性,其值不是 Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)。

備註

在執行階段使用這個方法加入新的 RichTextContentControl,此控制項是以文件中的原生內容控制項為基礎。 當您在執行階段建立 RichTextContentControl,而且希望下次開啟文件時再建立相同的控制項時,這個控制項很好用。 如需詳細資訊,請參閱在執行階段將控制項加入至 Office 文件

範例

下列程式碼範例會為文件中的每個原生 Rich Text 控制項建立新的 RichTextContentControl

這是示範文件層級自訂的版本。 若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中,並從 ThisDocument_Startup 方法呼叫 CreateRichTextControlsFromNativeControls 方法。

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
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);
        }
    }
}

這個版本適用於以 .NET Framework 4 或 .NET Framework 4.5 為目標的應用程式層級增益集。 若要使用這段程式碼,請將它貼到專案的 ThisAddIn 類別中,並從 ThisAddIn_Startup 方法呼叫 CreateRichTextControlsFromNativeControls 方法。

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
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);
        }
    }
}

下列程式碼範例會為使用者加入至文件的每個原生 Rich Text 控制項建立新的 RichTextContentControl

這是示範文件層級自訂的版本。 若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中。 若為 C#,您還必須將 ThisDocument_RichTextContentControlAfterAdd 事件處理常式附加至 ThisDocument 類別的 ContentControlAfterAdd 事件。

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
void ThisDocument_RichTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
    {
        this.Controls.AddRichTextContentControl(NewContentControl,
            "RichTextControl" + NewContentControl.ID);
    }
}

這個版本適用於以 .NET Framework 4 或 .NET Framework 4.5 為目標的應用程式層級增益集。 若要使用這段程式碼,請將它貼到專案的 ThisAddIn 類別中。 此外,您還必須將 ActiveDocument_RichTextContentControlAfterAdd 事件處理常式附加至現用文件的 ContentControlAfterAdd 事件。

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
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);
    }
}

.NET Framework 安全性

請參閱

參考

ControlCollection 介面

AddRichTextContentControl 多載

Microsoft.Office.Tools.Word 命名空間

其他資源

在執行階段將控制項加入至 Office 文件

如何:將內容控制項加入至 Word 文件