Manipulating Text Frame Orientation in Office 2010
Programmatically Working with Shapes in Office 2010: Learn how to manipulate a Microsoft Word 2010 document by changing the text in a shape.
Applies to: Microsoft Word 2010
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:
Add a Standard Module to a Word Document
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
Start Word 2010.
On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.
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.
On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.
Add the Code to 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
In the Projects pane, click Module1.
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
Test the Solution
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
Drag the Visual Basic Editor to the right side of the screen.
Next, drag the Word window to the left side of the screen and adjust both windows until you can see both.
Now, place the cursor in the WorkWithTextFrame module, press F8 to step through the code line-by-line.