Shape picture goes to unexpected position, even with correct .top and .left values - Word VBA

Viewed 1333

The problem is, I'm creating a new shape picture to replace a previous one. However, even tough the new shape properties Top and Left are exactly the same as the previous picture, the new shape gets dislocated. This happens on a header shape. I believe there is some sort of hidden property I'm not able to find on VBA watch.

I'm adding the code as this (A simplified version):

For i = ActiveDocument.Sections(1).Headers(1).Shapes.count To 1 Step -1

'shape to delete:
    set shp2del = ActiveDocument.Sections(1).Headers(1).Shapes(i)
'shape to add
Set shp2 = ActiveDocument.Sections(1).Headers(1).Shapes.AddPicture(replace_path, False, True, shp2del.Left, shp2del.Top, shp2del.width, shp2del.height)

shp2del.delete

next i

And voilá, it creates the object shp2, deletes shp2del.. with a small problem, in some documents, it doesn't stay in position.

I tried many things, redefining shp2.top and shp2.left after creating, looked for relative positions properties, anchor object properties, compared with the shape to delete and new shape, and everything seems to be the same unless I'm (most likely) missing a property.

I also tried to create a macro and see the code that would change the new shape into position, but unfortunately while creating a macro word doesn't allow to handle pictures very well..

The only hint I found was this one, but to change this programmatically, I don't know how.

old shape on left, new shape on right Update:

After the answer from m4o_tim the options that word displays are more accurate, still the distances remain incorrect on the final document

old shape on left, new one on right

I'm also curious about the lock anchor and allow overlap, but this is surely hidden within shp2.anchor and I'll try those out.

Regarding the absolute position I couldn't find a property that I could set. Thanks for the help m4o_tim, is there anyone else who can help?

Update 2: Finally its working, basically what's needed to do is to re-define top and left positions after the settings

                        With newshp
                        .WrapFormat.Type = oldshp.WrapFormat.Type
                        .RelativeHorizontalPosition = oldshp.RelativeHorizontalPosition
                        .LeftRelative = oldshp.LeftRelative
                        .RelativeVerticalPosition = oldshp.RelativeVerticalPosition
                        .TopRelative = oldshp.TopRelative
                        .Top = oldshp.Top
                        .Left = oldshp.Left
                        .LockAnchor = oldshp.LockAnchor
                        End With

After this it was working perfectly. Thanks m4o_tim

Update 3: I realized this luckily worked in only one section. The reason for such is the anchor position.

If I compare shp2del.Anchor.Information(wdVerticalPositionRelativeToPage) with shp2.Anchor.Information(wdVerticalPositionRelativeToPage)

I will get different values. I will open up a new question about this. Thanks everyone.

1 Answers

When adding your new shape you haven't specified the relative position so Word applies its default, relative to column and paragraph.

When you come across things in the Object Model that you are unfamiliar with try using the Object Browser and online help documentation.

Your code should be something like this:

Edit: Included setting anchor from old image

'shape to add
Set shp2 = ActiveDocument.Sections(1).Headers(1).Shapes.AddPicture(replace_path, False, True, , , shp2del.width, shp2del.height, shp2del.Anchor)
With shp2
    .WrapFormat.Type = shp2del.WrapFormat.Type
    .RelativeHorizontalPosition = shp2del.RelativeHorizontalPosition
    .Left = shp2del.Left
    .RelativeVerticalPosition = shp2del.RelativeVerticalPosition
    .Top = shp2del.Top
End With
Related