Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Position a textbox in Microsoft Word with VBA

Posted on 2013-11-13
3
Medium Priority
?
1,512 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

877 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