Solved

Position a textbox in Microsoft Word with VBA

Posted on 2013-11-13
3
1,222 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now