Wrong value for object properties READ for drawing objects in Word 2013/2010 VBA

Errors when reading a Property

I set a box in a canvas (box 2).

If I ask or change the Left and the Top property of this box using the box item in the CanvasItem collection, the values are displayed with a scale of 20 points.

If I read the box properties using a For Each iteration in the CanvasItems collection, the values are shown in points!

Run the first macro from the VBA editor, and read the Debug window.

Moreover

If I select the forms from the canvas, and I use the property ChildShapeRange, the measure are shown with a scale of 20, either using the item or a For Each iteration!

---- CODE -----

Option Explicit

Sub CoordinatesInTheCanvas()
    Dim canvas As Shape, box As Shape
    
    'Add a new box 1 on the document
    Set box = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, nbsp;   With box
        .TextFrame.TextRange.Text = CStr(1)
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
        .RelativeVerticalPosition = wdRelativeVerticalPositionPage
        .Name = "box 1"
    End With
    
    'Add a canvas on the document
    Set canvas = ActiveDocument.Shapes.AddCanvas(nbsp;   With canvas
        .Name = "canvas"
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
        .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    End With
    
    'Add a new box 2 in the canvas
    Set box = canvas.CanvasItems.AddTextbox(Orientation:=msoTextOrientationHorizontal, nbsp;   box.TextFrame.TextRange.Text = CStr(2): box.Name = "box 2"
    
    'Name and position of the forms on the document
    Debug.Print "Forms on the document"
    Dim form As Shape
    For Each form In ActiveDocument.Shapes
        Debug.Print form.Name, , form.Left, form.Top
    Next form

    'Name and position of the box in the canvas
    Debug.Print "Forms in the canvas"
    
    'Using collection CanvasItems : left and top are measured in points (read and write)
    Dim InCanO As Object 'object in the canvas ; iteration failed if InCan is declared as a Shape !
    For Each InCanO In canvas.CanvasItems
        Debug.Print InCanO.Name; "(For next)", InCanO.Left, InCanO.Top
        InCanO.Left = InCanO.Left + 10
        Debug.Print InCanO.Name; "(Moved in the for next)", InCanO.Left, InCanO.Top
    Next InCanO
    
    'Without using collection CanvasItems : left and top are measures with a scale of 20 points
    Dim InCanS As Shape
    Set InCanS = canvas.CanvasItems("box 2")
    Debug.Print InCanS.Name; "(Direct)", InCanS.Left, InCanS.Top
    InCanS.Left = InCanS.Left - 1
    Debug.Print InCanS.Name; "(Moved and direct)", InCanS.Left, InCanS.Top
    
'    ActiveDocument.Shapes("box 1").Delete
'    ActiveDocument.Shapes("canvas").Delete

    Set canvas = Nothing: Set box = Nothing: Set form = Nothing: Set InCanO = Nothing: Set InCanS = Nothing
   Child (True)
End Sub
Sub Child(x As Boolean)
    'Using child forms collection
    Dim InCan As Shape
    
    ActiveDocument.Shapes("canvas").CanvasItems.SelectAll
    Debug.Print "Selecting canvas forms"
        
    'Using collection ChildShapeRange
    For Each InCan In Selection.ChildShapeRange 'here, Incan can be declared as a Shape !!
        Debug.Print InCan.Name; "(Selected for next)", InCan.Left, InCan.Top
    Next InCan
    
    'not using collection ChildShapeRange
    Set InCan = Selection.ChildShapeRange("box 2")
    Debug.Print InCan.Name; "(direct selected)", InCan.Left, InCan.Top
End Sub


July 30th, 2015 9:49am

Hi Jrme

Your code does not compile: it's missing non-optional parameters in the AddTextBox and AddCanvas methods (there may be other problems, but execution doesn't get that far).

Please post working code to repro your problem - which is anyway not clearly formulated.

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 10:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics