Поделиться через


ControlCollection.AddPictureContentControl Method (ContentControl, String)

Adds a new PictureContentControl that is based on a native content control in the document.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

Syntax

'Declaration
Function AddPictureContentControl ( _
    contentControl As ContentControl, _
    name As String _
) As PictureContentControl
PictureContentControl AddPictureContentControl(
    ContentControl contentControl,
    string name
)

Parameters

Return Value

Type: Microsoft.Office.Tools.Word.PictureContentControl
The PictureContentControl that was added to the document.

Exceptions

Exception Condition
ArgumentNullException

contentControl is nulla null reference (Nothing in Visual Basic).

-or-

name is nulla null reference (Nothing in Visual Basic) or has zero length.

ControlNameAlreadyExistsException

A control with the same name is already in the ControlCollection.

ArgumentException

contentControl is not a building block gallery (that is, the Type property of contentControl does not have the value Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlPicture).

Remarks

Use this method to add a new PictureContentControl that is based on a native content control in the document at run time. This is useful when you create a PictureContentControl at run time, and you want to recreate the same control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

Examples

The following code example creates a new PictureContentControl for every native picture control that is in the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the CreatePictureControlFromNativeControl method from the ThisDocument_Startup method.

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

Private Sub CreatePictureControlsFromNativeControls()
    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.wdContentControlPicture Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PictureContentControl = _
                Me.Controls.AddPictureContentControl(nativeControl, _
                "VSTOPictureContentControl" + count.ToString())
            pictureControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.PictureContentControl> pictureControls;

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

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

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlPicture)
        {
            count++;
            Microsoft.Office.Tools.Word.PictureContentControl tempControl =
                this.Controls.AddPictureContentControl(nativeControl,
                "VSTOPictureContentControl" + count.ToString());
            pictureControls.Add(tempControl);
        }
    }
}

This version is for an application-level add-in that targets the .NET Framework 4. To use this code, paste it into the ThisAddIn class in your project, and call the CreatePictureControlFromNativeControl method from the ThisAddIn_Startup method.

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

Private Sub CreatePictureControlsFromNativeControls()
    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.wdContentControlPicture Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PictureContentControl = _
                vstoDoc.Controls.AddPictureContentControl(nativeControl, _
                "VSTOPictureContentControl" + count.ToString())
            pictureControls.Add(tempControl)
        End If
    Next nativeControl
End Sub
private System.Collections.Generic.List
   <Microsoft.Office.Tools.Word.PictureContentControl> pictureControls;

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

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

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

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlPicture)
        {
            count++;
            Microsoft.Office.Tools.Word.PictureContentControl tempControl =
                vstoDoc.Controls.AddPictureContentControl(nativeControl,
                "VSTOPictureContentControl" + count.ToString());
            pictureControls.Add(tempControl);
        }
    }
}

The following code example creates a new PictureContentControl for every native picture control that the user adds to the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project. For C#, you must also attach the ThisDocument_PictureContentControlAfterAdd event handler to the ContentControlAfterAdd event of the ThisDocument class.

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

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlPicture Then
        Me.Controls.AddPictureContentControl(NewContentControl, _
            "PictureControl" + NewContentControl.ID)
    End If
End Sub
void ThisDocument_PictureContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlPicture)
    {
        this.Controls.AddPictureContentControl(NewContentControl,
            "PictureControl" + NewContentControl.ID);
    }
}

This version is for an application-level add-in that targets the .NET Framework 4. To use this code, paste it into the ThisAddIn class in your project. Also, you must attach the ActiveDocument_PictureContentControlAfterAdd event handler to the ContentControlAfterAdd event of the active document.

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

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

.NET Framework Security

See Also

Reference

ControlCollection Interface

AddPictureContentControl Overload

Microsoft.Office.Tools.Word Namespace

Other Resources

Adding Controls to Office Documents at Run Time

Helper Methods for Host Controls

How to: Add Content Controls to Word Documents