Share via


ControlCollection.AddPictureContentControl Methode

Definition

Überlädt

AddPictureContentControl(Range, String)

Fügt ein neues PictureContentControl im angegebenen Bereich im Dokument hinzu.

AddPictureContentControl(String)

Fügt bei der aktuellen Auswahl im Dokument ein neues PictureContentControl hinzu.

AddPictureContentControl(ContentControl, String)

Fügt ein neues PictureContentControl hinzu, das auf einem nativen Inhaltssteuerelement im Dokument basiert.

AddPictureContentControl(Range, String)

Fügt ein neues PictureContentControl im angegebenen Bereich im Dokument hinzu.

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

Parameter

range
Range

Ein Range, der die Grenzen für das neue Steuerelement bereitstellt.

name
String

Der Name des neuen Steuerelements.

Gibt zurück

Das PictureContentControl, das dem Dokument hinzugefügt wurde.

Ausnahmen

name ist null oder hat die Länge 0 (null).

Ein Steuerelement mit dem gleichen Namen ist bereits in der ControlCollection enthalten.

Beispiele

Im folgenden Codebeispiel wird am Anfang des Dokuments ein neues PictureContentControl hinzugefügt. In diesem Beispiel wird davon ausgegangen, dass eine Datei mit dem Namen picture.bmp im Ordner %UserProfile%\Meine Dokumente (für Windows XP und früher) oder im Ordner %UserProfile%\Documents (für Windows Vista) vorhanden ist.

Diese Version ist für eine Anpassung auf Dokumentebene vorgesehen. Um diesen Code zu verwenden, fügen Sie ihn in die ThisDocument -Klasse in Ihrem Projekt ein, und rufen Sie die AddPictureControlAtRange -Methode aus der ThisDocument_Startup -Methode auf.

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

Diese Version ist für ein Add-In auf Anwendungsebene vorgesehen, das auf die .NET Framework 4 oder die .NET Framework 4.5 ausgerichtet ist. Um diesen Code zu verwenden, fügen Sie ihn in die ThisAddIn -Klasse in Ihrem Projekt ein, und rufen Sie die AddPictureControlAtRange -Methode aus der ThisAddIn_Startup -Methode auf.

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

Hinweise

Verwenden Sie diese Methode, um zur Laufzeit ein neues PictureContentControl in einem angegebenen Bereich im Dokument hinzuzufügen. Weitere Informationen finden Sie unter Adding Controls to Office Documents at Run Time.

Gilt für:

AddPictureContentControl(String)

Fügt bei der aktuellen Auswahl im Dokument ein neues PictureContentControl hinzu.

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

Parameter

name
String

Der Name des neuen Steuerelements.

Gibt zurück

Das PictureContentControl, das dem Dokument hinzugefügt wurde.

Ausnahmen

name ist null oder hat die Länge 0 (null).

Ein Steuerelement mit dem gleichen Namen ist bereits in der ControlCollection enthalten.

Beispiele

Im folgenden Codebeispiel wird am Anfang des Dokuments ein neues PictureContentControl hinzugefügt. In diesem Beispiel wird davon ausgegangen, dass eine Datei mit dem Namen picture.bmp im Ordner %UserProfile%\Meine Dokumente (für Windows XP und früher) oder im Ordner %UserProfile%\Documents (für Windows Vista) vorhanden ist.

Diese Version ist für eine Anpassung auf Dokumentebene vorgesehen. Um diesen Code zu verwenden, fügen Sie ihn in die ThisDocument -Klasse in Ihrem Projekt ein, und rufen Sie die AddPictureControlAtSelection -Methode aus der ThisDocument_Startup -Methode auf.

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

Diese Version ist für ein Add-In auf Anwendungsebene vorgesehen, das auf die .NET Framework 4 oder die .NET Framework 4.5 ausgerichtet ist. Um diesen Code zu verwenden, fügen Sie ihn in die ThisAddIn -Klasse in Ihrem Projekt ein, und rufen Sie die AddPictureControlAtSelection -Methode aus der ThisAddIn_Startup -Methode auf.

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

Hinweise

Verwenden Sie diese Methode, um zur Laufzeit ein neues PictureContentControl bei der aktuellen Auswahl im Dokument hinzuzufügen. Weitere Informationen finden Sie unter Adding Controls to Office Documents at Run Time.

Gilt für:

AddPictureContentControl(ContentControl, String)

Fügt ein neues PictureContentControl hinzu, das auf einem nativen Inhaltssteuerelement im Dokument basiert.

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

Parameter

contentControl
ContentControl

Das ContentControl, das die Grundlage für das neue Steuerelement ist.

name
String

Der Name des neuen Steuerelements.

Gibt zurück

Das PictureContentControl, das dem Dokument hinzugefügt wurde.

Ausnahmen

contentControl ist null.-or- name ist null oder hat die Länge null.

Ein Steuerelement mit dem gleichen Namen ist bereits in der ControlCollection enthalten.

contentControlist kein Bausteinkatalog (d. h. die Type -Eigenschaft von contentControl hat nicht den Wert Microsoft.Office.Interop.Word. WdContentControlType.wdContentControlPicture).

Beispiele

Im folgenden Codebeispiel wird ein neues PictureContentControl für jedes native Bildsteuerelement erstellt, das sich im Dokument befindet.

Diese Version ist für eine Anpassung auf Dokumentebene vorgesehen. Um diesen Code zu verwenden, fügen Sie ihn in die ThisDocument -Klasse in Ihrem Projekt ein, und rufen Sie die CreatePictureControlFromNativeControl -Methode aus der ThisDocument_Startup -Methode auf.

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

Diese Version ist für ein Add-In auf Anwendungsebene vorgesehen, das auf die .NET Framework 4 oder die .NET Framework 4.5 ausgerichtet ist. Um diesen Code zu verwenden, fügen Sie ihn in die ThisAddIn -Klasse in Ihrem Projekt ein, und rufen Sie die CreatePictureControlFromNativeControl -Methode aus der ThisAddIn_Startup -Methode auf.

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

Im folgenden Codebeispiel wird ein neues PictureContentControl für jedes native Bildsteuerelement erstellt, das der Benutzer dem Dokument hinzufügt.

Diese Version ist für eine Anpassung auf Dokumentebene vorgesehen. Um diesen Code zu verwenden, fügen Sie ihn in die ThisDocument -Klasse in Ihrem Projekt ein. Für C# müssen Sie den ThisDocument_PictureContentControlAfterAdd Ereignishandler auch an das ContentControlAfterAdd Ereignis der ThisDocument -Klasse anfügen.

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

Diese Version ist für ein Add-In auf Anwendungsebene vorgesehen, das auf die .NET Framework 4 oder die .NET Framework 4.5 ausgerichtet ist. Um diesen Code zu verwenden, fügen Sie ihn in die ThisAddIn -Klasse in Ihrem Projekt ein. Außerdem müssen Sie den ActiveDocument_PictureContentControlAfterAdd Ereignishandler an das ContentControlAfterAdd Ereignis des aktiven Dokuments anfügen.

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

Hinweise

Verwenden Sie diese Methode, um ein neues PictureContentControl hinzuzufügen, das auf einem nativen Inhaltssteuerelement im Dokument zur Laufzeit basiert. Dies ist nützlich, wenn Sie ein PictureContentControl zur Laufzeit erstellen und dasselbe Steuerelement beim nächsten Öffnen des Dokuments neu erstellen möchten. Weitere Informationen finden Sie unter Adding Controls to Office Documents at Run Time.

Gilt für: