none
Anchoring shapes to a specific range RRS feed

  • Question

  • Hello,

    I have fairly complex code that we use to highlight changes in publications. Basically, it adds vertical lines in the left margin based on a currently selected range of text. A bit like the "Track Changes" line, but as shapes.

    I have been using this code since Office 1997 without any problems, that is, until Office 2013...

    As I said, the code is complex because there are so many factors to be considered... Is the range in a header/footer? Is there a table? What are the "Space Before" paragraph settings? Is HTML spacing on? How wide is the left margin? etc.

    However, it seem the problem is this line of code:

            Set myShape = ActiveDocument.Shapes.AddLine(1, 1, 1, 10, rngCurrent.Paragraphs(1).Range)

    Word 2013 totally ignores the stuff in brackets. It used to be that the coordinates "1,1,1,10" were relative to "rngCurrent.Paragraphs(1).Range". No longer. Word 2013 ignores the Anchor parameter and uses the coordinates as if they were relative to the page and then proceeds to place the anchor next to the first paragraph that is not in a table on the said page.

    How do I get it to anchor "myShape" to "rngCurrent.Paragraphs(1).Range"?

    Thank you for any assistance.

    Thursday, August 13, 2015 5:34 PM

Answers

  • I have no idea why it might have been changed, or even if it was intentional. That said, try something along the lines of:

    Set myShape = ActiveDocument.Shapes.AddLine(1, 1, 1, 10, rngCurrent.Paragraphs(1).Range)
    With myShape
      .LockAnchor = True
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = rngCurrent.ParagraphFormat.SpaceBefore
      If rngCurrent.Information(wdWithInTable) = True Then
        .Anchor.Move rngCurrent.Start - .Anchor.Start
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionInnerMarginArea
        .Left = 10
      Else
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      End If
    End With

    Fei: There is (and always has been) an Anchor property; it's just not one of the .AddLine parameters.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 14, 2015 4:33 AM

All replies

  • A simple workaround is to insert the object as an inlineshape in the paragraph concerned, then convert it to a shape and lock the anchor. That way, the anchor will remain with the paragraph.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 14, 2015 2:10 AM
  • Hi Paul, thanks for the idea.

    I did think about that, but I could not find an easy Method connected to the InLineShapes collection that would make this easy. The only one I can see is the

    AddHorizontalLineStandard

    method.

    Maybe I can flip it vertically after I convert it to a shape...

    But really, why did they have to go and change that basic behaviour... It's one of those unintentional changes that they will claim is on purpose...

    Friday, August 14, 2015 2:47 AM
  • Hi Jean_Guy Marcil,

    As far as I know, there is no 'Anchor' parameter in Office 2007, Office 2010, Office 2013 Word object model. Here is the methoed link for your reference:
    Shapes.AddLine Method (Word)

    To anchor a line to the specific range, we can get the postion of range then set the parameter of Shapes.AddLine based on the postion of range. Here is an example that anchor the line to the specific range for your reference:

    x = Selection.Range.Information(wdHorizontalPositionRelativeToPage)
    y = Selection.Range.Information(wdVerticalPositionRelativeToPage)
    
    Set myShape = ActiveDocument.Shapes.AddLine(x, y, x + 100, y + 100, Selection.Range)

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 14, 2015 3:08 AM
    Moderator
  • I have no idea why it might have been changed, or even if it was intentional. That said, try something along the lines of:

    Set myShape = ActiveDocument.Shapes.AddLine(1, 1, 1, 10, rngCurrent.Paragraphs(1).Range)
    With myShape
      .LockAnchor = True
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = rngCurrent.ParagraphFormat.SpaceBefore
      If rngCurrent.Information(wdWithInTable) = True Then
        .Anchor.Move rngCurrent.Start - .Anchor.Start
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionInnerMarginArea
        .Left = 10
      Else
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      End If
    End With

    Fei: There is (and always has been) an Anchor property; it's just not one of the .AddLine parameters.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 14, 2015 4:33 AM
  • Hi Paul and Fei,

    Putting the tow together I got something going...:

    Dim MyShape As Shape
    Dim rngCurrent As Range
    Dim x As Long
    Dim y As Long
    
    Set rngCurrent = Selection.Range
    
    x = rngCurrent.Information(wdHorizontalPositionRelativeToPage)
    y = rngCurrent.Information(wdVerticalPositionRelativeToPage)
    
    Set MyShape = ActiveDocument.Shapes.AddLine(x, y, x, y + 10, rngCurrent)
    
    MyShape.LockAnchor = True
        
    rngCurrent.Select
    

    So later on I'll plug this in my existing code and see how it goes, I'll let you know if everything is fine or if I need more input...

    Thanks you so much!

    Friday, August 14, 2015 1:24 PM