Share via


ControlCollection.AddPlainTextContentControl, méthode (ContentControl, String)

Ajoute un nouveau PlainTextContentControl basé sur un contrôle de contenu natif dans le document.

Espace de noms :  Microsoft.Office.Tools.Word
Assembly :  Microsoft.Office.Tools.Word (dans Microsoft.Office.Tools.Word.dll)

Syntaxe

'Déclaration
Function AddPlainTextContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As PlainTextContentControl
PlainTextContentControl AddPlainTextContentControl(
    ContentControl contentControl,
    string name
)

Paramètres

Valeur de retour

Type : Microsoft.Office.Tools.Word.PlainTextContentControl
PlainTextContentControl ajouté au document.

Exceptions

Exception Condition
ArgumentNullException

contentControl a la valeur nullune référence null (Nothing en Visual Basic).

ou

name est nullune référence null (Nothing en Visual Basic) ou une longueur nulle.

ControlNameAlreadyExistsException

Un contrôle du même nom figure déjà dans ControlCollection.

ArgumentException

contentControl n'est pas une galerie de blocs de construction (à savoir la propriété Type de contentControl n'a pas la valeur Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText).

Notes

Utilisez cette méthode pour ajouter un nouveau PlainTextContentControl basé sur un contrôle de contenu natif dans le document au moment de l'exécution.Cela est utile lorsque vous créez un PlainTextContentControl au moment de l'exécution et que vous souhaitez recréer le même contrôle lors de la prochaine ouverture du document.Pour plus d'informations, consultez Ajout de contrôles à des documents Office au moment de l'exécution.

Exemples

L'exemple de code suivant crée un nouveau PlainTextContentControl pour chaque contrôle de texte brut natif figurant dans le document.

Cette version est destinée à une personnalisation au niveau du document.Pour utiliser ce code, collez-le dans la classe ThisDocument de votre projet, puis appelez la méthode CreateTextControlsFromNativeControls à partir de la méthode ThisDocument_Startup.

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

Private Sub CreatePlainTextControlsFromNativeControls()
    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.wdContentControlText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PlainTextContentControl = _
                Me.Controls.AddPlainTextContentControl(nativeControl, _
                "VSTOPlainTextContentControl" + count.ToString())
            plainTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List<Microsoft.Office.Tools.Word.PlainTextContentControl> plainTextControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlText)
        {
            count++;
            Microsoft.Office.Tools.Word.PlainTextContentControl tempControl =
                this.Controls.AddPlainTextContentControl(nativeControl,
                "VSTOPlainTextContentControl" + count.ToString());
            plainTextControls.Add(tempControl);
        }
    }
}

Cette version s'adresse un complément d'application qui cible .NET Framework 4 ou .NET Framework 4.5.Pour utiliser ce code, collez-le dans la classe ThisAddIn de votre projet, puis appelez la méthode CreateTextControlsFromNativeControls à partir de la méthode ThisAddIn_Startup.

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

Private Sub CreatePlainTextControlsFromNativeControls()
    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.wdContentControlText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PlainTextContentControl = _
                vstoDoc.Controls.AddPlainTextContentControl(nativeControl, _
                "VSTOPlainTextContentControl" + count.ToString())
            plainTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List<Microsoft.Office.Tools.Word.PlainTextContentControl> plainTextControls;

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

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

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlText)
        {
            count++;
            Microsoft.Office.Tools.Word.PlainTextContentControl tempControl =
                vstoDoc.Controls.AddPlainTextContentControl(nativeControl,
                "VSTOPlainTextContentControl" + count.ToString());
            plainTextControls.Add(tempControl);
        }
    }
}

L'exemple de code suivant crée un nouveau PlainTextContentControl pour chaque contrôle de texte brut natif que l'utilisateur ajoute au document.

Cette version est destinée à une personnalisation au niveau du document.Pour utiliser ce code, collez-le dans la classe ThisDocument de votre projet.En C#, vous devez également attacher le gestionnaire d'événements ThisDocument_PlainTextContentControlAfterAdd à l'événement ContentControlAfterAdd de la classe ThisDocument.

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

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlText Then
        Me.Controls.AddPlainTextContentControl(NewContentControl, _
            "PlainTextControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_PlainTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlText)
    {
        this.Controls.AddPlainTextContentControl(NewContentControl,
            "PlainTextControl" + NewContentControl.ID);
    }
}

Cette version s'adresse un complément d'application qui cible .NET Framework 4 ou .NET Framework 4.5.Pour utiliser ce code, collez-le dans la classe ThisAddIn de votre projet.Vous devez également attacher le gestionnaire d'événements ActiveDocument_PlainTextContentControlAfterAdd à l'événement ContentControlAfterAdd du document actif.

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlText Then
        vstoDoc.Controls.AddPlainTextContentControl(NewContentControl, _
            "PlainTextControl" + NewContentControl.ID)
    End If
End Sub
void ActiveDocument_PlainTextContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlText)
    {
        vstoDoc.Controls.AddPlainTextContentControl(NewContentControl,
            "PlainTextControl" + NewContentControl.ID);
    }
}

Sécurité .NET Framework

Voir aussi

Référence

ControlCollection Interface

AddPlainTextContentControl, surcharge

Microsoft.Office.Tools.Word, espace de noms

Autres ressources

Ajout de contrôles à des documents Office au moment de l'exécution

Comment : ajouter des contrôles de contenu à des documents Word