PDA

View Full Version : Solved: Textbox with 2 paragraphs



MK_
12-04-2010, 06:13 AM
Hi all,

I wish to create a textbox (shape) with more than one paragraph with diferent formats (alignment, font, etc).
I´ve tried to use TextFrame.TextRange.InsertParagraphBefore and TextFrame.TextRange.InsertParagraphAfter to achieve it, with no results.
Each time TextFrame.TextRange.Text is used the whole text inside the textbox is replaced. No idea how to proceed.
I´m using MS Word 2003.
Sample code:
Public Sub TxBox()
Dim oTextBox As Shape
Set oTextBox = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, _
Top:=0, _
Width:=InchesToPoints(3), _
Height:=InchesToPoints(1), _
Anchor:=ActiveDocument.Paragraphs(1).Range)
With oTextBox
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = InchesToPoints(3)
.Top = InchesToPoints(4)
.TextFrame.TextRange.Text = "Paragraph one"
With .TextFrame.TextRange.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
'
' Here should come the second paragraph
'
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.Line.Visible = msoTrue
.Fill.Visible = msoFalse
End With
End Sub
Thanks in advance.

gmaxey
12-04-2010, 06:41 AM
Create the new paragraph with the .Range.Text method:
Public Sub TxBox()
Dim oTextBox As Shape
Set oTextBox = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=InchesToPoints(3), Height:=InchesToPoints(1), _
Anchor:=ActiveDocument.Paragraphs(1).Range)
With oTextBox
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = InchesToPoints(3)
.Top = InchesToPoints(4)
With .TextFrame
.MarginTop = 0
.MarginLeft = 0
With .TextRange
With .Paragraphs(1).Range
.Text = "Paragraph one" & vbCr 'add the second paragraph here
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
With .Paragraphs(2).Range
.Text = "Paragraph two"
.Font.Color = wdColorBlue
End With
End With
End With
.Line.Visible = msoTrue
.Fill.Visible = msoFalse
End With
End Sub

MK_
12-04-2010, 07:28 AM
Thanks Greg!

Paul_Hossler
12-04-2010, 07:30 AM
Greg -- I was trying to see how you could reference .Paragraphs(2) without inserting it

Then I saw your " ..& vbCR" for the text in the first paragraph

Pretty clever; I would have done it the hard way

Paul

gmaxey
12-04-2010, 07:51 AM
Paul,

I don't know if it was clever or not. It was just what came to mind after I realized that I had to do something ;-)

Since it wasn't evident it probably wasn't that clever and thinking about it now I would probably do it like:

With .TextRange
With .Paragraphs(1).Range
.Text = "Paragraph one"
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
.Paragraphs.Add
With .Paragraphs(2).Range
.Text = "Paragraph two"
.Font.Color = wdColorBlue
End With
End With

Is that the hard way? ;-)