centering line of text following shape

H

hlngus

I'm wanting to force the text of the line following any shapes to be
centered.

My feeble attempt is below.

How can I accomplish this?

Here's a snippet:

If ActiveDocument.Shapes.Count <> 0 Then
For i = 1 To ActiveDocument.Shapes.Count
If left(ActiveDocument.Shapes(i).name, 4) = "PICT" _
Then
ActiveDocument.Shapes(i).Select
Selection.HomeKey unit:=wdLine
With Selection.Paragraphs
.Alignment = wdAlignParagraphCenter
End With
End If
Next
End If

Thanks.
 
D

Doug Robbins - Word MVP on news.microsoft.com

If the shape has a name that matches the condition, the following will
centre align the following paragraph (if there is one)

Dim arng As Range
With ActiveDocument
If .Shapes.Count <> 0 Then
For i = 1 To .Shapes.Count
If Left(.Shapes(i).Name, 4) = "PICT" Then
Set arng = .Range
.Shapes(i).Select
arng.Start = Selection.Range.End
Selection.Collapse wdCollapseEnd
With arng
If .Paragraphs.Count > 1 Then
.Paragraphs(2).Alignment = wdAlignParagraphCenter
End If
End With
End If
Next
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
H

hlngus

That was very helpful, thanks:

Below is the final snippet that was used.

Dim arng As Range
With ActiveDocument
If .Shapes.Count <> 0 Then
For i = 1 To .Shapes.Count
If left(.Shapes(i).name, 4) = "PICT" And Val
(AdOrder.Class) >= 1400 And Val(AdOrder.Class) <= 1699 Then
Set arng = .Range
.Shapes(i).Select
arng.Start = Selection.Range.End
Selection.Collapse wdCollapseEnd
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious
Selection.Paragraphs.Alignment =
wdAlignParagraphCenter
End If
Next
End If
End With

It's curious that the wdGoToNext actually takes me past the line I
want to center,
so I had to back up 1 line.
 
D

Doug Robbins - Word MVP on news.microsoft.com

Instead of using the GoTo, I would suggest that you use the code that I gave
you as it tests to see if there is a paragraph for which the alignment can
be set.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
H

hlngus

Point well taken. I did have an issue with the paragraph not being
selected, which caused an unrelated problem further in the code.
 

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