Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Replace style in next paragraph

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ingeborg Altern Vedal - 01 Mar 2005 08:29 GMT
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
Helmut Weber - 01 Mar 2005 10:25 GMT
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
Ingeborg Altern Vedal - 01 Mar 2005 15:23 GMT
> Hi Ingeborg,
>
[quoted text clipped - 5 lines]
> "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)
Greg Maxey - 01 Mar 2005 10:33 GMT
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?

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi
> I'm new to news, so I hope I do this the right way. Previous I've
[quoted text clipped - 19 lines]
>
> Ingeborg Altern Vedal
Stefan Blom - 01 Mar 2005 11:09 GMT
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.

--
Stefan Blom

> Ingeborg,
>
[quoted text clipped - 41 lines]
> >
> > Ingeborg Altern Vedal
Greg Maxey - 01 Mar 2005 11:15 GMT
Stefan,

Yes, I think you are right.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> I think the OP wants to *apply* a certain style to any paragraph
> immediately following a heading. This makes sense, because it is
[quoted text clipped - 50 lines]
>>>
>>> Ingeborg Altern Vedal
Ingeborg Altern Vedal - 01 Mar 2005 15:25 GMT
> Stefan,
>
> Yes, I think you are right.

That's right. But thanks anyway :-)

Ingeborg
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.