Compartilhar via


How to: Programmatically add pictures and Word Art to documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can add pictures and drawing objects to your documents at design time or during run time. WordArt enables you to add decorative text to Microsoft Office Word documents. These special text effects are drawing objects that you can customize and insert into your document.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

Add a picture at design time

If you are developing a document-level customization, you can add a picture to the document at design time.

To add a picture to a Word document at design time

  1. Place your cursor where you want to insert the picture in the document.

  2. Click the Insert tab of the ribbon.

  3. In the Illustrations group, click Picture.

  4. In the Insert Picture dialog box, navigate to the picture you want to insert, and click Insert.

    The picture is added to your document at the current cursor location.

Add a picture at run time

You can insert a picture into a document at the current cursor location.

To add a picture at the cursor location

  1. Call the AddPicture method of the InlineShapes collection and pass in the name of the file.

    Me.Application.Selection.InlineShapes.AddPicture("C:\SamplePicture.jpg")
    
    this.Application.Selection.InlineShapes.AddPicture(@"C:\SamplePicture.jpg");
    

Add WordArt at design time

If you are developing a document-level customization, you can add WordArt to the document at design time.

To add WordArt to a Word document at design time

  1. Place your cursor where you want to insert the WordArt in the document.

  2. Click the Insert tab of the Ribbon.

  3. In the Text group, click WordArt, and then select a WordArt style.

  4. Add the text that you want to appear in the document to the Edit WordArt Text dialog box and click OK.

    The text is added to your document with the selected WordArt style applied.

Add WordArt at run time

You can insert WordArt into a document at the current cursor location. The procedure is different for document-level customizations and VSTO Add-ins.

To add WordArt at the cursor location in a document-level customization

  1. Get the left and top position of the current cursor location.

    Dim leftPosition As Double = Me.Application.Selection.Information( _
        Word.WdInformation.wdHorizontalPositionRelativeToPage)
    
    Dim topPosition As Double = Me.Application.Selection.Information( _
        Word.WdInformation.wdVerticalPositionRelativeToPage)
    
    float leftPosition = (float)this.Application.Selection.Information[
        Word.WdInformation.wdHorizontalPositionRelativeToPage];
    
    float topPosition = (float)this.Application.Selection.Information[
        Word.WdInformation.wdVerticalPositionRelativeToPage];
    
  2. Call the AddTextEffect method of the Shapes object in the document.

    Me.Shapes.AddTextEffect( _
        Office.MsoPresetTextEffect.msoTextEffect29, "SampleText", _
        "Arial Black", 24, _
        Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, _
        leftPosition, topPosition)
    
    this.Shapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect29, "SampleText",
        "Arial Black", 24, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse,
        leftPosition, topPosition);
    

To add WordArt at the cursor location in a VSTO Add-in

  1. Get the left and top position of the current cursor location.

    Dim leftPosition As Double = Me.Application.Selection.Information( _
        Word.WdInformation.wdHorizontalPositionRelativeToPage)
    
    Dim topPosition As Double = Me.Application.Selection.Information( _
        Word.WdInformation.wdVerticalPositionRelativeToPage)
    
    float leftPosition = (float)this.Application.Selection.Information[
        Word.WdInformation.wdHorizontalPositionRelativeToPage];
    
    float topPosition = (float)this.Application.Selection.Information[
        Word.WdInformation.wdVerticalPositionRelativeToPage];
    
  2. Call the AddTextEffect method of the Shapes object of the active document (or a different document that you specify).

    Me.Application.ActiveDocument.Shapes.AddTextEffect( _
        Office.MsoPresetTextEffect.msoTextEffect29, "SampleText", _
        "Arial Black", 24, Office.MsoTriState.msoFalse, _
        Office.MsoTriState.msoFalse, leftPosition, topPosition)
    
    this.Application.ActiveDocument.Shapes.AddTextEffect(
        Office.MsoPresetTextEffect.msoTextEffect29, "SampleText",
        "Arial Black", 24, Office.MsoTriState.msoFalse, 
        Office.MsoTriState.msoFalse, leftPosition, topPosition);
    

Compile the code

  • A picture named SamplePicture.jpg must exist on drive C.

See also