Solved

Position a textbox in Microsoft Word with VBA

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

740 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