Export (0) Print
Expand All

Working with the Crop Property and Shapes in Office 2010

Office 2010

Office Quick Note banner

Programmatically Working with Shapes in Office 2010: Learn how to update a Microsoft Word 2010 chart by changing various settings of the title.

Last modified: April 26, 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

Microsoft Office 2010 includes the ability to work with many kinds of art shapes. For example, some examples of shapes include lines, connectors, basic shapes, flowchart elements, stars and banners, and callouts. You can improve and manipulate the appearance of shapes programmatically with various classes and object. In this topic, you programmatically create an image and then use various properties to crop the image. 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 Customize 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 an image. The code then sets various properties of the Corp property object.

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 CropDemo()
        ' Put your own image path in here. This image path
        ' should work for standard Windows 7 installations.
        Const fileName As String = "C:\Windows\Web\Wallpaper\Landscapes\img10.jpg"
           
       
        With ActiveDocument
            Dim shp As Shape
            Set shp = ActiveDocument.Shapes.AddPicture(fileName, msoFalse, msoTrue)
          
            shp.Line.Visible = True
           
            ' Retrieve the dimensions of the shape:
            Dim picWidth As Double
            Dim picHeight As Double
            picWidth = shp.Width
            picHeight = shp.Height
           
            With shp.PictureFormat.Crop
                ' Modify the picture itself, not its container:
                ' Shift the picture 10% to the right and down,
                ' and then make the picture 90% of its original size.
                .PictureOffsetX = picWidth / 10
                .PictureOffsetY = picHeight / 10
                .PictureHeight = picHeight * 0.9
                .PictureWidth = picWidth * 0.9
               
                ' Now modify the cropping by changing the shape
                ' of the container. Changing the shape alters
                ' the portion of the picture that you see:
                .ShapeHeight = 100
                .ShapeWidth = 100
                .ShapeLeft = 100
                .ShapeTop = 150
            End With
        End With
    End Sub
    
    

In the following steps, you step through the code that creates a picture and then uses various properties of the Crop property object to crop the image. 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 CropDemo module and then press F8 to step through the code line-by-line.

Show:
© 2014 Microsoft