Replace style in next paragraph

  • Thread starter Ingeborg Altern Vedal
  • Start date
I

Ingeborg Altern Vedal

Hi
I'm new to news, so I hope I do this the right way. Previous I've found
many «recipes» by using Google's groups. Now, however I need specific help.

I've made many macros in Word by recording, but I can't figure out how
to do this one:

I'm preparing a book for print. I would like to change paragraphs from
one style to another if the previous paragraph is a heading.
1. Find paragraph in style "Heading 1"
2. Go to next paragraph
3. I paragraph is in style "Normal" change style to "Normal 2"
4. If paragraph is not "Normal" go to next "Heading 1"
5. Do the same (1.-4.) with "Heading 2", "Heading 3" etc

If I replace all paragraphs successor to "Heading 1" to "Normal 2" I
will end up replacing all paragraphs, including "Heading 2" etc.
(The difference between "Normal" and "Normal 2" is indent/ no indent and
amount of space before paragraph.)

I appreciate any suggestions, esp. a whole VBA code!

Ingeborg Altern Vedal
 
H

Helmut Weber

Hi Ingeborg,

a rather slow but simple solution
and untested (!) solution could be:

Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If Left(oPrg.Range.Style, 7 = "Heading") Then
If oPrg.Next.Style = "normal" Then
oPrg.Next.Style = "normal 2"
End If
Next
Next

If speed is important, then I'd use
something like (pseudocodish!):

for l = 1 to 4
with .find
.style = "Heading " & cstr(l)
while .execute
set paragraphs next style
wend
end with
next

HTH

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
G

Greg Maxey

Ingeborg,

Maybe I misundertand, but it sounds like you want to change all "normal"
paragraphs to "normal1" paragraphs. Right?

If you want to use VBA, then maybe something like:

Sub LoopAndDoSomething()

Dim oPara As Paragraph
Set oPara = ActiveDocument.Paragraphs(1)

Do
If oPara.Style = "Normal" Then
oPara.Style = "Body Text"
End If
Set oPara = oPara.Next
Loop Until oPara Is Nothing
End Sub

However, why not just redefine "normal" for your document?
 
S

Stefan Blom

I think the OP wants to *apply* a certain style to any paragraph
immediately following a heading. This makes sense, because it is
customary not to use first line indents for the first paragraph after
each heading.
 
I

Ingeborg Altern Vedal

Hi Ingeborg,

a rather slow but simple solution
and untested (!) solution could be:

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000

Helmut,
*Thank you!* You made my day! I had to change it a little bit, but now
it works excellent. All of a sudden work that just to take days may be
accomplished in one minute!

Here is my code:

Sub endreStil()
'
' endreStil Makro
' Makro laget 01.03.2005 av Ingeborg Altern Vedal
'
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Range.Style = "Overskrift 1" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Overskrift 2" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Overskrift 3" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Overskrift 4" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Sitat" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Sitat m innrykk" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal u innrykk"
End If
End If
If para.Range.Style = "Petit" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal etter petit"
End If
End If
If para.Range.Style = "Petit u innrykk" Then
If para.Next.Style = "Normal" Then
para.Next.Style = "Normal etter petit"
End If
End If

Next

End Sub

Ingeborg (Norway)
 

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