Formatting caption to match the previous paragraph

H

hgoslin

Hi
I would appreciate some help in coding a macro that finds each instance
of a caption style in a document and sets the caption to the same left
indent value as its preceding paragraph.

My Caption style is called ARTableCaption
My 3 possible paragraph styles which preceed each caption are:
ARBodyText Level1 Left Indent 1 cm
ARBodyText Level2 Left Indent 2 cm
ARBodyText Level3 Left Indent 3.25 cm

This is my code so far, but it is not working.

With ActiveDocument.Content.Find
.Style = "ARTableCaption"

Do While .Execute(FindText:="", Forward:=True, _
Format:=True) = True
Selection.MoveUp Unit:=wdLine, Count:=1

If Selection.Style = ActiveDocument.Styles("ARBodyText Level1")
Then
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
With Selection.ParagraphFormat
.LeftIndent = CentimetersToPoints(1)
.RightIndent = CentimetersToPoints(0)
End With
End If
End With

....
Loop

Thanks
Heather
 
H

Helmut Weber

Hi Heather,

to get You going, here an example to get the style
of a paragraph preceding a paragraph with style "normal".

Forget about selection.

"On error" or some other construct is necessary,
as You'll get an error, if the first paragaph style is "normal",
because there is no preceding paragraph, and as a result,
no character "previous".

"Previous" returns the letter previous to what your
search for style "normal" has found.

Sub Test78()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
ResetSearch
On Error Resume Next
With rngDcm.Find
.Style = "normal"
While .Execute
MsgBox rngDcm.Previous.Style
Wend
End With
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

hgoslin

Thanks Helmut
With a little bit of digging in help after your code showed how to find
the previous style, I have come up with the following code, based on
your example. I know I could make it loop through the styles rather
than do multiple if's but it works for me:
Sub FormatTableCaptions()
'
' FormatTableCaptions Macro
''
Dim rngDcm As Range
Dim myPrev As String

Set rngDcm = ActiveDocument.Range

With rngDcm.Find
.ClearFormatting
.Style = "ARTableCaption"

Do While .Execute(FindText:="", Forward:=True, _
Format:=True) = True
myPrev = rngDcm.Previous.Style

ResetSearch
On Error Resume Next

If myPrev = "ARBodyText Level1" Then
With rngDcm.Next.Style.ParagraphFormat
.LeftIndent = CentimetersToPoints(1)
End With
End If
If myPrev = "ARBodyText Level2" Then
With rngDcm.Next.Style.ParagraphFormat
.LeftIndent = CentimetersToPoints(2)
End With
End If
If myPrev = "ARBodyText Level3" Then
With rngDcm.Next.Style.ParagraphFormat
.LeftIndent = CentimetersToPoints(3.25)
End With
End If
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Move Unit:=wdParagraph, Count:=1
End With

Loop
End With

End Sub

Greetings from sunny South Africa

Thanks for the help
Heather
 

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