1

I'm writing a simple code to position my shapes (which are actually pictures) in the document. I want them to be positioned:

  • horizontally to exactly 0 mm. from the left side of the printable area

  • vertically to 7 mm. below the paragraph (to which the shape is anchored)

I wrote a simple code:


Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.Left = MillimetersToPoints(0)
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
Selection.ShapeRange.Top = MillimetersToPoints(7)

Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom

For 1 shape on the page it works fine. But if there are more then 1 shape, it somehow "throws" the 2nd shape to the top of the page. It looks like Word anchors it to the 1st paragraph on the page. but it shouldn't. At the same time horizontal positioning is ok.

I would appreciate any help to fix this issue.

My possible solution for this issue will look as follows:

Sub PositShape_3()
Dim I As Integer

If Selection.InlineShapes.Count <> 0 Then
  For I = Selection.InlineShapes.Count To 1 Step -1
    Selection.InlineShapes(I).ConvertToShape
  Next I
End If

Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.Left = MillimetersToPoints(0)
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine
Selection.ShapeRange.Top = MillimetersToPoints(7)

Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom

End Sub    

In spite of the fact that the use of wdRelativeVerticalPositionLine solved the problem, it is still interesting why the use of wdRelativeVerticalPositionParagraph has such unexpected unwanted consequences.

2
  • You might have the result you describe if the properties of the shapes don't allow overlapping. Commented Jan 4, 2018 at 1:46
  • I set Selection.ShapeRange.WrapFormat.AllowOverlap = False, but actually it hasn't solved the problem. The issue persists. Commented Jan 17, 2018 at 10:24

1 Answer 1

1

Note the use of SELECTION in the code you show us. If you don't change the paragraph selection, then the shapes will always be anchored to the same paragraph. Working with a Selection in Word is tricky; it's much better to work with a more tangible object, such as a specific paragraph.

The following code sample illustrates using paragraph objects to anchor and position successively added Shapes.

Sub insertShapesProgressively()
  Dim shp As word.Shape
  Dim shpRng As word.ShapeRange
  Dim rng As word.Range
  Dim iParaCounter As Long

  'We want to insert the Shape anchored to three different paragraphs
  ' on the same page
  For i = 7 To 9
    Set rng = ActiveDocument.Paragraphs(i).Range
    Set shp = ActiveDocument.shapes.AddShape(msoShapeWave, 0, 0, 10, 10, rng)
    Set shpRng = rng.ShapeRange
    shpRng.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    shpRng.Left = MillimetersToPoints(0)
    shpRng.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    shpRng.Top = MillimetersToPoints(7)
    shpRng.WrapFormat.Type = wdWrapTopBottom
  Next
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the response. I think it's true, if I would realize it as a part of Loop. I created my macro just as a single separate program. Each time I manually select 1 (one) definite paragraph, insert a picture (shape) and then try to precisely position it using this small single macro. So, each time I definitely select a different paragraph and I do change my selection of paragraph by hand. I think that it cannot be the same paragraph each time. Instead of wdRelativeVerticalPositionParagraph I tried to use wdRelativeVerticalPositionLine. And it works fine now, it solved the problem.
Sorry, I don't know how to insert code here correctly
Click the EDIT link at the very end of your question. That will let you make changes and add (readable!) code :-)
@Dmytro If my answer did help you solve your problem you should mark it as "the answer" by clicking the checkmark. This will hlep others with similar questions recognize the contribution as helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.