Export (0) Print
Expand All

Manipulating Text Frame Orientation in Office 2010

Office 2010

Office Quick Note banner

Programmatically Working with Shapes in Office 2010: Learn how to manipulate a Microsoft Word 2010 document by changing the text in a shape.

Last modified: April 27, 2011

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
Add a Standard Module to a Word Document
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   April 2011

Provided by:    Frank Rice, Microsoft Corporation

All objects placed on a page in a Microsoft Word 2010 document have an invisible bounding box or frame around the object. Text frames hold text. In this topic, you programmatically manipulate the text frame in a shape. To complete this task, you must do the following:

In this task, you open a Word 2010 document, open the Visual Basic Editor, and then insert a standard module.

To add a standard module to a Word document

  1. Start Word 2010.

  2. On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.

    Note Note

    If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.

  3. On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.

In this task, you add programming code that creates the simple shape, adds a frame containing text, and then manipulates that frame.

To add code to the Visual Basic Editor

  1. In the Projects pane, click Module1.

  2. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub WorkWithTextFrame()
        Dim shp As Shape
        With ActiveDocument.Shapes
            Set shp = .AddShape(MsoAutoShapeType.msoShapeOval, 10, 10, 400, 200)
            With shp.TextFrame
                .TextRange.Text = "Here's my shape!"
               
                ' Set the location of the anchor: in Word
                ' note that the anchor is at the top center.
                .HorizontalAnchor = msoAnchorCenter
                .VerticalAnchor = msoAnchorTop
               
                ' Note that the text doesn't rotate with the shape.
                ' Unless you set this to msoTrue, when you rotate the
                ' shape, the text rotates as well.
                ' Try commenting out this line and running the code
                ' to see how it affects the behavior.
                .NoTextRotation = msoTrue
               
                ' Set the path format. You can use only one of
                ' the built-in formats, or msoPathTypeNone, which
                ' removes the path:
                .PathFormat = msoPathType2
               
                ' Modify the warping of the text, using
                ' one of the built-in warp formats:
                .WarpFormat = msoWarpFormat12
            End With
            shp.Rotation = 45
            shp.Top = 100
        End With
    End Sub
    
    

In the following steps, you step through the code and watch the shape being created, text added, and then the text frame being manipulated. The best way to see the code in action is to place the Visual Basic Editor window and the Word window side-by-side.

To step through the code

  1. Drag the Visual Basic Editor to the right side of the screen.

  2. Next, drag the Word window to the left side of the screen and adjust both windows until you can see both.

  3. Now, place the cursor in the WorkWithTextFrame module, press F8 to step through the code line-by-line.

Show:
© 2014 Microsoft