Inserting Table in TextBox

J

JBNewsGroup

Hi



I have a requirement to draw a Rectangle (using the drawing object) and to
place text centerd

within the Rectangle. I have been able to accomplish this by drawing the
Rectangle, placing a

TextBox within it and then placing a 1 cell Table within that.



I was only able to insert the table by first selecting the Textbox. How do I
set the Table

range (TextBox) without showing that the TextBox was selected? I have tried
various methods

without any success. I am missing something but am not sure what I am
missing. If I do have

to show the "select", how do I "un-select"?



My test code is as follows:

----------------------------------------------------------------------------
---------------------------

Sub TestRectangle()

'

' In Final Version Get Rectangle dimensions

' and location from from Input Form

'

Dim RectL As Integer

Dim RectT As Integer

Dim RectW As Integer

Dim RectH As Integer



Dim BoxL As Integer

Dim BoxT As Integer

Dim BoxW As Integer

Dim BoxH As Integer



Dim RectName As String

Dim TextBoxName As String

Dim InsideText As String



InsideText = "This" & vbCrLf & "Is" & vbCrLf & "Inside" & vbCrLf &
"Text"



RectL = 100

RectT = 20

RectW = 25

RectH = 41



With ActiveDocument.Shapes

BoxL = MillimetersToPoints(RectL)

BoxT = MillimetersToPoints(RectT)

BoxW = MillimetersToPoints(RectW)

BoxH = MillimetersToPoints(RectH)



RectName = .AddShape (msoShapeRectangle, _

BoxL, BoxT, BoxW, BoxH).Name



BoxL = MillimetersToPoints(RectL + 1) ' recalculate, seems
to center Text

BoxT = MillimetersToPoints(RectT + 1) ' box more accurately

BoxW = MillimetersToPoints(RectW - 2)

BoxH = MillimetersToPoints(RectH - 2)



TextBoxName = .AddTextbox(msoTextOrientationHorizontal, _

BoxL, BoxT, BoxW,
BoxH).Name



With .Range(TextBoxName)

.Line.Visible = msoFalse



With .TextFrame

.MarginBottom = 0

.MarginLeft = 0

.MarginRight = 0

.MarginTop = 0



With .TextRange

.Font.Bold = wdToggle

.Font.Name = "Arial"

.Font.Size = 9 ' In final version
adjust text to fit in TextBox

End With

End With

End With



Call PutTextBoxTable (TextBoxName, BoxW, BoxH, InsideText)



.Range (Array(RectName, TextBoxName)).Group

End With

End Sub

----------------------------------------------
----------------------------

Private Sub PutTextBoxTable (TextBoxName As String, _

BoxW As Integer, BoxH As
Integer, _

BoxText As String)



ActiveDocument.Shapes.Range(TextBoxName).Select ' need something
different?



ActiveDocument.Tables.Add _

Range:=Selection.Range, _

NumRows:=1, NumColumns:=1, _

AutoFitBehavior:=wdAutoFitFixed



With Selection.Tables(1)

.Rows.Alignment = wdAlignRowCenter

.TopPadding = 0

.BottomPadding = 0

.LeftPadding = 0

.RightPadding = 0

.AllowAutoFit = False

.AllowPageBreaks = False

.Borders(wdBorderLeft).LineStyle = wdLineStyleNone

.Borders(wdBorderRight).LineStyle = wdLineStyleNone

.Borders(wdBorderTop).LineStyle = wdLineStyleNone

.Borders(wdBorderBottom).LineStyle = wdLineStyleNone



With .Range

.Rows.SetHeight RowHeight:=BoxH, HeightRule:=wdRowHeightExactly

.Rows.AllowBreakAcrossPages = False



.Cells.SetWidth ColumnWidth:=BoxW, RulerStyle:=wdAdjustNone

.Cells.VerticalAlignment = wdCellAlignVerticalCenter



.ParagraphFormat.Alignment = wdAlignParagraphCenter

.Text = BoxText

End With

End With

End Sub

----------------------------------------------------------------------------
---------------------------



Thanks in advance for any help.



Jerry Bodoff
 
J

Jezebel

First, why bother with the rectangle -- why not just set the textbox border?
And why the one-cell table -- why not just center the text horizontally and
vertically within the textbox itself?

But anyway, the trick is not to use the Selection object. Macro code is
nearly always more reliable if you work exclusively with Range objects --

Dim pTextbox As Word.Shape
Dim pRange As Word.Range
Dim pTable As Word.Table

Set pTextbox =
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, L, T, W, H)
Set pRange = pTextbox .TextFrame.TextRange
Set pTable = pRange .Tables.Add(pRange, 1,1)
 
J

JBNewsGroup

Hi Jezebel,

Thank you for your response and tip about using the Range object. I have
to apologize for all the double spacing in my question. I am using Outlook
Express as a newsreader for the first time and right now I have no clue as
why messages double space. I really preferred the old newsgroup format.
The new one for tired eyes is miserable.

To answer your questions:

1. I am using a rectangle because later on I will have other shapes, i.e.;
circle triangle, diamond.

2. Truthly, I used a 1 cell table since I could not find a way to center
text vertically in the text box
by any other means. How do you vertically center text in a textbox? I
am using WORD 2000 if
that makes any difference.

Once again thanks for your help and tip.

Jerry Bodoff
 

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

Top