Solved

Position a textbox in Microsoft Word with VBA

Posted on 2013-11-13
3
1,296 Views
Last Modified: 2013-11-22
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
0
Comment
Question by:thenelson
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 39649195
The textbox appears to be in the same place in both cases.  

I don't see you selecting the right place to insert the textbox in code.  Perhaps you could use a bookmark, or search for "Date of visit" and go down one line, and to the left, before inserting it.  Sometimes it helps to record a macro and see what code results.
0
 
LVL 39

Author Comment

by:thenelson
ID: 39649466
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.
0
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39668980
thanks for your response
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This collection of functions covers all the normal rounding methods of just about any numeric value.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question