0
votes

I aim to replace a certain text in a ms word document with a textbox. This textbox needs to have the replaced text in it. The textbox should appear at the same position like the replaced text and the following content must wrap around the textbox.

With the following code I can accomplish the replacement of the text with a textbox and with the replaced text in the textbox, but the textbox is always at the same place.

What I need now, is the possibility to get the position of a found text to use it for the textbox to replace the found text. :)

My code snippet:

positionLeft = 50  /* here I need the left position of the found text */
positionTop = 50   /* and here the top position */

With ActiveDocument.Range.find
            .Forward = True
            .Wrap = wdFindStop
            .Text = TextToReplace
            .Execute
            If .Found = True Then
                Dim Box As Shape
                Set Box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, positionToLeft, positionToRight, 100, 100)
                Box.TextFrame.textRange.Text = TextToReplace
                .Parent = ""
            End If
End With

Is it even possible to get the absolute left and top position of a text in the word document? And if yes, how can i get this done?

1

1 Answers

0
votes

You can use Selection.Information(wdVerticalPositionRelativeToPage), Selection.Information(wdHorizontalPositionRelativeToPage) to find the coordinates but it is just relative to that page.

I haven't tested this but if you add

            .Select 
            x = Selection.Information(wdVerticalPositionRelativeToPage)
            y = Selection.Information(wdHorizontalPositionRelativeToPage)
            Set Box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal,x, y, 100, 100)

It should work.