Formating Text in Textframes

D

Dylan

I am able to use the for each - next method to run through textframes, and
if a frame with a width greater than 200 is encountered I am able to set the
left and right indentations.

However, I can seem to be able to change the alignment of the text where the
string "N/A" is found inside the frame. Am I going about this the correct
way?

This will let me set the indentation, and following it is the code I have
written to try to select textframes with the string "N/A"

Sub FormatTextbox()
Dim shp As Shape
Dim Sel As Selection

' Switch Screen Updating off
Word.Application.ScreenUpdating = False

For Each shp In ActiveDocument.Shapes
' Format Plain Textboxes
If shp.Width > 240 Then
shp.Select
With Selection
.ParagraphFormat.LeftIndent = CentimetersToPoints(0.25)
.ParagraphFormat.RightIndent = CentimetersToPoints(0.14)
.ParagraphFormat.Alignment = wdAlignParagraphJustify
.Range.Font.Name = "Times New Roman"
.Range.Font.Size = 10
End With
' [Following Code goes here]
End If

'[Following Code]

Next shp

End Sub
ElseIf shp.TextFrame.TextRange.Text = "*[N/A]*" Then
shp.Select
With Selection
' Set text to centred alignment
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.MoveLeft Unit:=wdCharacter, Count:=3
.TypeParagraph

' Set the font size to 10
.Font.Size = 10
.ShapeRange.Select
.Font.Size = 10
End With

Any help would be appreciated.

Dylan
 
D

Doug Robbins - Word MVP

Hi Dylan,

The .Text property of a shape includes the paragraph mark ¶. Therefore you
have to use "*[N/A]*" & vbCr in the ElseIf statement.

It is better to apply the formatting directly to the object rather than to
select the object and apply it to the Selection. It will be quicker and you
don't then need to turn off the ScreenUpdating

Dim shp As Shape

For Each shp In ActiveDocument.Shapes
If shp.Width > 240 Then
With shp.TextFrame.TextRange
.ParagraphFormat.LeftIndent = CentimetersToPoints(0.25)
.ParagraphFormat.RightIndent = CentimetersToPoints(0.14)
.ParagraphFormat.Alignment = wdAlignParagraphJustify
.Font.Name = "Times New Roman"
.Font.Size = 10
End With
ElseIf shp.TextFrame.TextRange.Text = "*[N/A]*" & vbCr Then
With shp.TextFrame.TextRange
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Text = vbCr & shp.TextFrame.TextRange.Text 'I assume this is
what your code was doing
.Font.Size = 10
End With
End If
Next shp


--
Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
D

Dylan

I keep getting an error saying this object does not support attached text.

Is there a way to make it ignore the current textframe and go to the next if
the object encountered doesn't support attached text.
 

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