ControlCollection.AddPictureContentControl-Methode (Range, String)

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

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

Syntax

'Declaration
Function AddPictureContentControl ( _
    range As Range, _
    name As String _
) As PictureContentControl
PictureContentControl AddPictureContentControl(
    Range range,
    string name
)

Parameter

Rückgabewert

Typ: Microsoft.Office.Tools.Word.PictureContentControl
Das PictureContentControl, das dem Dokument hinzugefügt wurde.

Ausnahmen

Ausnahme Bedingung
ArgumentNullException

name ist nullNULL-Verweis (Nothing in Visual Basic) oder hat die Länge 0 (null).

ControlNameAlreadyExistsException

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

Hinweise

Verwenden Sie diese Methode, um in einem angegebenen Bereich im Dokument zur Laufzeit ein neues PictureContentControl hinzuzufügen. Weitere Informationen finden Sie unter Hinzufügen von Steuerelementen zu Office-Dokumenten zur Laufzeit.

Beispiele

Im folgenden Codebeispiel wird am Anfang des Dokuments ein neues PictureContentControl hinzugefügt. In diesem Beispiel wird davon ausgegangen, dass sich eine Datei mit dem Namen picture.bmp im Ordner %UserProfile%\My Documents (für Windows XP und frühere Windows-Versionen) bzw. im Ordner %UserProfile%\Documents (für Windows Vista) befindet.

Diese Version bezieht sich auf eine Anpassung auf Dokumentebene. Zum Verwenden dieses Codes fügen Sie ihn in der ThisDocument-Klasse in das Projekt ein und rufen in der ThisDocument_Startup-Methode die AddPictureControlAtRange-Methode auf.

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

Diese Version bezieht sich auf ein Add-In auf Anwendungsebene, das auf .NET Framework 4 abzielt. Zum Verwenden dieses Codes fügen Sie ihn in der ThisAddIn-Klasse in das Projekt ein und rufen in der ThisAddIn_Startup-Methode die AddPictureControlAtRange-Methode auf.

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

.NET Framework-Sicherheit

Siehe auch

Referenz

ControlCollection Schnittstelle

AddPictureContentControl-Überladung

Microsoft.Office.Tools.Word-Namespace

Weitere Ressourcen

Hinzufügen von Steuerelementen zu Office-Dokumenten zur Laufzeit

Hilfsmethoden für Hoststeuerelemente

Gewusst wie: Hinzufügen von Inhaltssteuerelementen zu Word-Dokumenten