Avatar of thenelson
thenelson
 asked on

Position a textbox in Microsoft Word with VBA

With the following code, I create a textbox around some text and try to anchor it in the correct spot:
With Selection
    .CreateTextbox
'    Selection.ShapeRange(1).Width = InchesToPoints(7.3)
    With .ShapeRange(1)
        .Width = InchesToPoints(7.3)
        .TextFrame.AutoSize = True
        .RelativeVerticalPosition = wdRelativeVerticalPositionLine
        .Anchor.Application.ActiveDocument.Bookmarks("textboxAnchor").Range.Select
    End With
End With

Open in new window

The bookmark "textboxAnchor" is located just before "Chief complaint" in the correct place image below. I tried putting the anchor in different places with no luck.

It all works well except the location of the textbox sometimes moves. Here is where I want the textbox:
Textbox- wrong place
Here is where the textbox often ends up (above "Date of visit" instead of below it):
Textbox- wrong place
Any idea how I can properly locate the textbox on the page using VBA?  TIA
Microsoft WordMicrosoft OfficeMicrosoft Applications

Avatar of undefined
Last Comment
thenelson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Helen Feddema

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
thenelson

ASKER
I uploaded two different images. For some reason the two images displayed are both the "correct place". I uploaded the "wrong place" hopefully below. wrong placeI am trying to anchor the textbox to a bookmark with:
    .Anchor.Application.ActiveDocument.Bookmarks("textboxAnchor").Range.Select
but it doesn't seem to be consistent.

After I posted this question, I added the line:
    .Top = InchesToPoints(0.1)
after the Anchor line above and that seems to work at least for now.

Thanks for your response.
thenelson

ASKER
thanks for your response
Your help has saved me hundreds of hours of internet surfing.
fblack61