Vertical positioning of a text box in VBA



Hello all,

I have a template which is formatted in three text columns. I need to define
three types of headers. One which is formatted over the width of three
columns all together, one over two columns and the last one over a single

The last one is not a problem, I use a style for this. I am having problems
with the first two headers. I don't want to keep adding sectionbreaks because
I find that you get mixed (read undesired) results when adding a new section
in the middle of a document.

Therefor I created some code that adds a text box with the title. I have a
function which checks which column the selection is in, another function
which counts the number of columns at the insertion point and with those I
calculate the width of the textbox I need.
So far so good. The thing I can't seem to get right is the vertical position
of the textbox. I want it to be anchored at the selection point, actually
right above it.
I use wdRelativeVerticalPositionParagraph for the property
RelativeVerticalPosition of the shape.
The result however, is not what I want. The textbox is always positioned in
one of the last lines of the previous paragraph and not in - or at the top of
- the paragraph I used to position the text box in.

This is a part of my code:

Sub TitleColumns(iNoOfDesiredColumns As Long)
Dim oRange As Range
Dim oTextBox As Shape
Dim sngWidth As Single
Dim sngHorPosition As Single
Dim iColumn As Long
Dim iNoOfColumns As Long
Const csngHeight As Long = 24

Set oRange = Selection.Paragraphs(1).Range
oRange.Collapse direction:=wdCollapseStart

'Function which gets the current column number
iColumn = GetCurrentColumn()

'Function which gets the total number of columns in the current range
iNoOfColumns = GetNoColumns(oRange)

'Function which determines the width of the textbox
sngWidth = DetermineWidht(oRange, iColumn, iNoOfDesiredColumns)

Set oTextBox = ActiveDocument.Shapes.AddTextbox
(msoTextOrientationHorizontal, 0, 0, sngWidth, csngHeight, oRange)

With oTextBox

.TextFrame.TextRange = "Text here"
.RelativeHorizontalPosition = wdRelativeHorizontalPositionCharacter
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.Line.Visible = msoTrue
.WrapFormat.DistanceTop = 0
.WrapFormat.DistanceBottom = 0
.WrapFormat.Type = wdWrapTopBottom
.WrapFormat.AllowOverlap = False
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
.LockAnchor = True
End With

End Sub
The general idea is to call it with TitleColumns x, where x is the number of
columns you want to put the header above.
As I said before, all works well apart from the vertical positioning of the
textbox. I don't want to position it relative to the page, but need it to be
at the top of the paragraph.

Do I need to calculate the height of the paragraph and substract the height
of text box and use that for the top property of the text box?
I would like to fix this otherwise if possible, because I can't be certain
of course that the text in the paragraph following the header won't be
changed after the header is created.

Any pointers on how to fix this or an alternative way to make this work?


P.s. The code is used in Word 2003


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question