Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

A shape may not appear in the expected location when you use a macro to insert the shape in Excel 2007


View products that this article applies to.

Symptoms

When you use a macro to add a shape to a workbook in Microsoft Office Excel 2007, the shape may not appear in the expected location.

↑ Back to the top


Cause

This behavior occurs because shapes are positioned in relation to the current view. When you use a macro to add a shape to a workbook, the macro defines specific coordinates for the position of the shape. However, the coordinates that are provided by the macro provide different results when you use different views in Excel 2007.

↑ Back to the top


Workaround

To work around this behavior, replace the code in the macro by using code that resembles the following.
Sub <MacroName>()
   Dim vw As XlWindowView
   If ActiveWindow.View <> xlNormalView Then
      vw = ActiveWindow.View
      ActiveWindow.View = xlNormalView
   End If   

   ActiveSheet.Shapes.AddShape 1, 800, 10, 100, 100
   
   If vw <> xlNormalView Then
      ActiveWindow.View = vw
   End If
End Sub
Notes
  • In this code sample, <MacroName> is the name of the macro.
  • The code sample guarantees that the view is set to normal before the shape is added to the view. After the shape is added to the view, the view is reset to the view that was used before the macro was started.

↑ Back to the top


More information

Steps to reproduce the behavior

  1. Start Excel 2007, and then press ALT+F11 to start the Microsoft Visual Basic Editor.
  2. On the Tools menu, click Macros, type TestShape, click Create, and then replace the code for the TestShape method with the following code.
    Sub TestShape()
        Sheet1.Shapes.AddShape 1, 800, 10, 100, 100
    End Sub
  3. On the File menu, click Close and Return to Microsoft Excel.
  4. On the View tab, click View Macros, click TestShape, and then click Run.

    Note The shape is displayed in column Q.
  5. On the View tab, click Page Layout.
  6. In the View tab, click View Macros, click TestShape, and then click Run.
The shape is displayed in column P.

↑ Back to the top


Keywords: KB931392, kbexpertisebeginner, kbprb, kbtshoot

↑ Back to the top

Article Info
Article ID : 931392
Revision : 2
Created on : 10/15/2007
Published on : 10/15/2007
Exists online : False
Views : 233