Formatting Text and Shapes Programmatically in Word 2010

Summary: Learn how to use Visual Basic for Applications (VBA) to programmatically manipulate and format shapes and special text, such as WordArt, pictures, and SmartArt text, in Word 2010.

Applies to: Office 2010 | Open XML | Visual Studio Tools for Microsoft Office | Word 2007 | Word 2010

In this article
Introduction
Applying picture styles programmatically
Applying text effects to a title or heading programmatically
Applying WordArt text effects to text in a SmartArt graphic programmatically
Conclusion
Additional Resources

Published: April 2012

Provided by: Saul Candib, Microsoft Corporation

Contents

  • Introduction

  • Applying picture styles programmatically

  • Applying text effects to a title or heading programmatically

  • Applying WordArt text effects to text in a SmartArt graphic programmatically

  • Conclusion

  • Additional Resources

Introduction

In Word 2010, new objects and members enable you to manipulate and format shapes and text programmatically, but in many cases, you cannot use the macro recorder to learn how to do it. In particular, neither can you select pictures or SmartArt while you are recording a macro, nor will the recorder pick up formatting changes to WordArt text. This article shows you how to avoid this situation, manipulate shapes, and apply effects programmatically.

The following table lists some of the new members that you can use to manipulate and format text shapes and pictures.

Member

Description

ColorFormat.Brightness Property (Word)

Returns a Single that represents the brightness of a specified shape color. Read/write.

FillFormat.GradientAngle Property (Word)

Returns or sets the angle of the gradient fill for the specified fill format. Read/write.

FillFormat.GradientStops Property (Word)

Returns the GradientStops collection associated with the specified fill format. Read-only.

FillFormat.PictureEffects Property (Word)

Returns a PictureEffects object that can be used to apply picture effects to the specified fill formatting. Read-only.

FillFormat.RotateWithObject Property (Word)

Returns or sets whether the fill rotates with the specified shape. Read/write.

FillFormat.TextureAlignment Property (Word)

Returns or sets the alignment (the origin of the coordinate grid) for the tiling of the texture fill. Read/write.

FillFormat.TextureHorizontalScale Property (Word)

Returns or sets a Single that specifies the horizontal scaling factor for the texture fill. Read/write.

FillFormat.TextureOffsetX Property (Word)

Returns or sets a Long that specifies the horizontal offset of the texture from the origin in points. Read/write.

FillFormat.TextureOffsetY Property (Word)

Returns or sets a Long that specifies the vertical offset of the texture from the origin in points. Read/write.

FillFormat.TextureTile Property (Word)

Returns or sets whether the texture fill is tiled or centered. Read/write.

FillFormat.TextureVerticalScale Property (Word)

Returns or sets a Single that specifies the vertical scaling factor for the texture fill. Read/write.

Font.Fill Property (Word)

Returns a FillFormat object that contains fill formatting properties for the font that was used by the specified range of text. Read-only.

Font.Glow Property (Word)

Returns a GlowFormat object that represents the glow formatting for the font that was used by the specified range of text. Read-only.

Font.Line Property (Word)

Returns a LineFormat object that specifies the formatting for a line. Read/write.

Font.Reflection Property (Word)

Returns a ReflectionFormat object that represents the reflection formatting for a shape. Read-only.

Font.TextColor Property (Word)

Returns a ColorFormat object that represents the color for the specified font. Read-only.

Font.TextShadow Property (Word)

Returns a ShadowFormat object that specifies the shadow formatting for the specified font.

Font.ThreeD Property (Word)

Returns a ThreeDFormat object that contains 3-D effect formatting properties for the specified font. Read-only.

Transparency Property

Returns or sets a Single that specifies the degree of transparency for the glow effect as a value between 0.0 (opaque) and 1.0 (clear). Read/write.

InlineShape.HasSmartArt Property (Word)

Returns True if there is a SmartArt diagram present on the shape. Read-only.

InlineShape.SmartArt Property (Word)

Returns a SmartArt object that provides a way to work with the SmartArt associated with the specified inline shape. Read-only.

InlineShapes.AddSmartArt Method (Word)

Inserts a SmartArt graphic as an inline shape into the active document.

PictureFormat.Crop Property (Word)

Returns or sets a Crop object that represents an image cropping. Read/write.

ReflectionFormat.Blur Property (Word)

Returns or sets a Single that specifies the degree of blur effect applied to the specified object. Read/write.

ReflectionFormat.Offset Property (Word)

Returns or sets the separation, in points, of the reflected image from the shape. Read/write.

ReflectionFormat.Transparency Property (Word)

Returns or sets a Single that specifies the degree of transparency for the reflection effect as a value between 0.0 (opaque) and 1.0 (clear). Read/write.

Shape.HasSmartArt Property (Word)

Returns True if there is a SmartArt diagram present on the shape. Read-only.

Shape.SmartArt Property (Word)

Returns a SmartArt object that lets you work with the SmartArt associated with the specified shape. Read-only.

Shapes.AddSmartArt Method (Word)

Inserts the specified SmartArt graphic into the active document.

SoftEdgeFormat.Radius Property (Word)

Returns or sets a Single that represents the length of the radius for a soft edge effect. Read/write.

TextFrame2.ThreeD Property (Office)

Returns a ThreeDFormat object that represents the three-dimensional formatting of the parent shape. Read-only.

TextFrame2.WarpFormat Property (Office)

Returns or sets the warp format (how the text is warped) for the specified text frame. Read/write.

TextFrame2.WordArtformat Property (Office)

Returns or sets the WordArt type for the specified text frame. Read/write.

Applying picture styles programmatically

Various UI controls on the Picture Format ribbon tab in Word 2010 let you apply styles and formats to pictures in your documents. For example, the Picture Styles gallery offers various designs you can use to frame your pictures. These designs incorporate several formatting aspects, such as border, line, rotation, glow, shadow, and 3-D. For more control, you can open the Format Picture dialog box, as shown in Figure 1.

Figure 1. The 3-D Format tab of the Format Picture dialog box

3-D Format tab of the Format Picture dialog box

The various tabs in this dialog box give you control over almost every facet of the picture.

You can also achieve all or most of the same formatting programmatically. One thing that you can't do, however, is use the Macro Recorder to determine just how to format pictures programmatically, because the recorder will not let you select a picture.

Consider the portrait of a young man shown in Figure 2.

Figure 2. A portrait of a young man before the application of picture styles

Portrait of a young man

The following code sample inserts the picture in the document at the selected location and applies some of the previously mentioned formatting to it. In particular, it applies a frame to the picture, applies reflection, sets an artistic picture effect, applies a 3-D extrusion to the frame, and then rotates the picture and adds perspective.

Public Sub AddPictureStyles()

    Dim WdSelection As Selection
    Dim WdInlineShape As InlineShape
    Dim WdShape As Shape
   
    Set WdSelection = Application.ActiveWindow.Selection
    Set WdInlineShape = WdSelection.InlineShapes.AddPicture("C:\Users\username\Pictures\portrait.jpg")
    Set WdShape = WdInlineShape.ConvertToShape
    
    ' Set the color type
    WdShape.PictureFormat.ColorType = msoPictureAutomatic
    
    ' Set the reflection type
    WdShape.Reflection.Type = msoReflectionType2
    
    ' Set the frame shape to oval
    WdShape.AutoShapeType = msoShapeOval
    WdShape.Line.Style = msoLineThickBetweenThin
    WdShape.Line.Weight = 10
    
    ' Set the artistic picture effect
    WdShape.Fill.PictureEffects.Insert (msoEffectPlasticWrap)
    
    ' Set the size and color of the frame extrusion
    WdShape.ThreeD.SetThreeDFormat (msoThreeD2)
    WdShape.ThreeD.ExtrusionColor = RGB(Red:=255, Green:=28, Blue:=0)
    
    ' Set the shape rotation and perspective
    WdShape.ThreeD.RotationX = 35
    WdShape.ThreeD.RotationY = 8
    WdShape.ThreeD.RotationZ = 357
    WdShape.ThreeD.Perspective = msoTrue
    
End Sub

The sample code uses several formatting-related objects to set picture properties and effects, including frame shape, frame extrusion, artistic picture effect, and shape rotation and perspective. Among these objects are the FillFormat, LineFormat, PictureFormat, ReflectionFormat, SoftEdgeFormat, and ThreeDFormat objects.

After you run the code, the picture resembles the image in Figure 3.

Figure 3. The same portrait after application of picture formatting

Same portrait after formatting

Applying text effects to a title or heading programmatically

In Word 2010, you can apply some very interesting text effects to titles or headings programmatically. For example, you can apply text effects such as reflection, shadow, and glow. For reflection, you can set four parameters: transparency; size; distance from text; and amount of blur. In addition, Word provides nine preset reflection types, which are combinations of the previous four parameters.

The following code sample shows how to apply some of these text effects to a heading. Before running the code, select the heading text you want to format.

Public Sub SetTextEffects()

    Dim WdSelection As Selection
    Set WdSelection = Application.ActiveWindow.Selection
    With WdSelection.Font
        .Size = 36
        .ColorIndex = wdDarkRed
        .Bold = True
        .Reflection.Type = msoReflectionType5
        .Glow.Radius = 5
        .TextShadow.Type = msoShadow5
    End With

End Sub

If your heading reads "This is a heading," after running this sample code, the heading resembles the one shown in Figure 4.

Figure 4. Heading text after application of text effects

Formatted heading text

Applying WordArt text effects to text in a SmartArt graphic programmatically

Word 2010 introduces several new ways to apply effects to SmartArt graphic text programmatically. These text-formatting effects are basically the same ones available for heading text, as described in the previous section.

The following sample code adds a SmartArt graphic to the document, formats it as WordArt, and applies text effects, including a warp format, to the SmartArt graphic.

Public Sub AddSmartArt()
    
    Dim WdShape As Shape
    Dim WdShapes As Shapes
    Dim WdSmartArtLayouts As SmartArtLayouts
    Dim WdSmartArtLayout As SmartArtLayout
    Set WdShapes = ActiveDocument.Shapes
    Set WdSmartArtLayouts = Application.SmartArtLayouts
    Set WdSmartArtLayout = WdSmartArtLayouts(81)
    Set WdShape = WdShapes.AddSmartArt(WdSmartArtLayout)
    WdShape.SmartArt.Nodes(1).TextFrame2.TextRange.Text = "This Way"
    WdShape.SmartArt.Nodes(2).TextFrame2.TextRange.Text = "That Way"
    WdShape.SmartArt.Nodes(1).TextFrame2.WordArtformat = msoTextEffect12
    WdShape.SmartArt.Nodes(1).TextFrame2.ThreeD.SetThreeDFormat msoThreeD10
    WdShape.SmartArt.Nodes(2).TextFrame2.TextRange.Font.Glow.Radius = 25
    WdShape.SmartArt.Nodes(2).TextFrame2.WarpFormat = msoWarpFormat13
    
End Sub

The resulting shape resembles the one shown in Figure 5.

Figure 5. A SmartArt graphic, with text formatted as WordArt

Smart Art graphic

SmartArt consists of a set of shapes in a graphic layout. Word provides about 150 of these layouts, all of which include text placeholders. You can see a preview of these layouts in the Choose a SmartArt Graphic dialog box, which you open by clicking SmartArt on the Insert tab in the Word UI.

You might have observed that I identified the SmartArt layout that I wanted to use (Diverging Arrows) by its index number (81) in the SmartArtLayouts collection:

    Set WdSmartArtLayout = WdSmartArtLayouts(81)

That is because there is no enumeration of these layouts in the object model. To find out which index number corresponds to which layout, you can use the following short code sample. To determine the layout that you want to apply, examine the layouts in the Choose a Smart Art Graphic dialog box in the Word UI. (On the Insert tab, in the Illustrations group, click SmartArt.) Then pass that index number to the SmartArtLayouts.Item method to determine the layout that you want. Be aware that because the Item method is the default method of the SmartArtLayouts collection, you do not need to write out the full syntax (SmartArtLayouts.Item()).

The following code example prints the names of the SmartArt layouts. The list of results is also shown in this blog post.

Public Sub GetSmartArtLayoutNames()

    Dim WdSmartArtLayouts As SmartArtLayouts
    Set WdSmartArtLayouts = Application.SmartArtLayouts
    For i = 1 To WdSmartArtLayouts.Count
    Debug.Print i; WdSmartArtLayouts(i).Name
    Next i

End Sub

Be aware that the constants in the MsoPresetTextEffect enumeration correspond to the WordArt formats in the WordArt gallery on the Format tab of SmartArt Tools on the ribbon, as shown in Figure 6. The constants, which are numbered one through thirty, correspond to the formats in that gallery, starting on the top row, going left to right, and then moving to the next row, and so on.

Figure 6. The WordArt Gallery

WordArt Gallery

Conclusion

This article shows how to use new objects and members in the Word 2010 VBA object model to format text, pictures, and SmartArt shapes programmatically.

Additional Resources

For more information, see the following resources: