Share via


ControlCollection.AddPictureContentControl Method

Definition

Overloads

AddPictureContentControl(Range, String)

Adds a new PictureContentControl at the specified range in the document.

AddPictureContentControl(String)

Adds a new PictureContentControl at the current selection in the document.

AddPictureContentControl(ContentControl, String)

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

AddPictureContentControl(Range, String)

Adds a new PictureContentControl at the specified range in the document.

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

Parameters

range
Range

A Range that provides the bounds for the new control.

name
String

The name of the new control.

Returns

The PictureContentControl that was added to the document.

Exceptions

name is null or has zero length.

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

Examples

The following code example adds a new PictureContentControl to the beginning of the document. This example assumes that a file named picture.bmp exists in the %UserProfile%\My Documents folder (for Windows XP and earlier) or the %UserProfile%\Documents folder (for Windows Vista).

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

private Microsoft.Office.Tools.Word.PictureContentControl pictureControl2;
private System.Drawing.Bitmap bitmap2;

private void AddPictureControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    pictureControl2 = this.Controls.AddPictureContentControl(
        this.Paragraphs[1].Range, "pictureControl2");

    string imagePath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + "\\picture.bmp";
    bitmap2 = new System.Drawing.Bitmap(imagePath, true);
    pictureControl2.Image = bitmap2;
}
Dim pictureControl2 As Microsoft.Office.Tools.Word.PictureContentControl
Dim bitmap2 As System.Drawing.Bitmap

Private Sub AddPictureControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    pictureControl2 = Me.Controls.AddPictureContentControl(Me.Paragraphs(1).Range, "pictureControl2")
    Dim imagePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
            "\picture.bmp"
    bitmap2 = New System.Drawing.Bitmap(imagePath, True)
    pictureControl2.Image = bitmap2
End Sub

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

private Microsoft.Office.Tools.Word.PictureContentControl pictureControl2;
private System.Drawing.Bitmap bitmap2;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    pictureControl2 = vstoDoc.Controls.AddPictureContentControl(
        vstoDoc.Paragraphs[1].Range, "pictureControl2");

    string imagePath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + "\\picture.bmp";
    bitmap2 = new System.Drawing.Bitmap(imagePath, true);
    pictureControl2.Image = bitmap2;
}
Dim pictureControl2 As Microsoft.Office.Tools.Word.PictureContentControl
Dim bitmap2 As System.Drawing.Bitmap

Private Sub AddPictureControlAtRange()
    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()
    pictureControl2 = vstoDoc.Controls.AddPictureContentControl( _
        vstoDoc.Paragraphs(1).Range, "pictureControl2")
    Dim imagePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
            "\picture.bmp"
    bitmap2 = New System.Drawing.Bitmap(imagePath, True)
    pictureControl2.Image = bitmap2
End Sub

Remarks

Use this method to add a new PictureContentControl at a specified range in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddPictureContentControl(String)

Adds a new PictureContentControl at the current selection in the document.

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

Parameters

name
String

The name of the new control.

Returns

The PictureContentControl that was added to the document.

Exceptions

name is null or has zero length.

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

Examples

The following code example adds a new PictureContentControl to the beginning of the document. This example assumes that a file named picture.bmp exists in the %UserProfile%\My Documents folder (for Windows XP and earlier) or the %UserProfile%\Documents folder (for Windows Vista).

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

private Microsoft.Office.Tools.Word.PictureContentControl pictureControl1;
private System.Drawing.Bitmap bitmap1;

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

    pictureControl1 = this.Controls.AddPictureContentControl("pictureControl1");

    string imagePath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + "\\picture.bmp";
    bitmap1 = new System.Drawing.Bitmap(imagePath, true);
    pictureControl1.Image = bitmap1;
}
Dim pictureControl1 As Microsoft.Office.Tools.Word.PictureContentControl
Dim bitmap1 As System.Drawing.Bitmap

Private Sub AddPictureControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    pictureControl1 = Me.Controls.AddPictureContentControl("pictureControl1")
    Dim imagePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
            "\picture.bmp"
    bitmap1 = New System.Drawing.Bitmap(imagePath, True)
    pictureControl1.Image = bitmap1
End Sub

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

private Microsoft.Office.Tools.Word.PictureContentControl pictureControl1;
private System.Drawing.Bitmap bitmap1;

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

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

    pictureControl1 = vstoDoc.Controls.AddPictureContentControl("pictureControl1");

    string imagePath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + "\\picture.bmp";
    bitmap1 = new System.Drawing.Bitmap(imagePath, true);
    pictureControl1.Image = bitmap1;
}
Dim pictureControl1 As Microsoft.Office.Tools.Word.PictureContentControl
Dim bitmap1 As System.Drawing.Bitmap

Private Sub AddPictureControlAtSelection()
    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()
    pictureControl1 = vstoDoc.Controls.AddPictureContentControl("pictureControl1")
    Dim imagePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
            "\picture.bmp"
    bitmap1 = New System.Drawing.Bitmap(imagePath, True)
    pictureControl1.Image = bitmap1
End Sub

Remarks

Use this method to add a new PictureContentControl at the current selection in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddPictureContentControl(ContentControl, String)

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

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

Parameters

contentControl
ContentControl

The ContentControl that is the basis for the new control.

name
String

The name of the new control.

Returns

The PictureContentControl that was added to the document.

Exceptions

contentControl is null.-or- name is null or has zero length.

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

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).

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

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

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

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.

void ThisDocument_PictureContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlPicture)
    {
        this.Controls.AddPictureContentControl(NewContentControl,
            "PictureControl" + NewContentControl.ID);
    }
}
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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. 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.

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

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.

Applies to