Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2007, 07:51 PM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional Find and Replace Styles

Hi
I have written code that goes through a long document and replaces one set of styles with another. I am using Find and Replace and it works OK, but I am stuck with a conditional bit that goes like this:
When it finds Heading 3, if the preceding paragraph style is Heading 1 or Body text, replace Heading 3 with Heading 2 (otherwise leave it)

This is the straightforward replacement, but it needs the conditional bit (I guess it needs a loop, but I just can't get it right)

      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Style = ActiveDocument.Styles("Heading 3")
        .Text = ""
        .Replacement.Style = ActiveDocument.Styles("Heading 2")
        .Execute Replace:=wdReplaceAll
    End With
 
Old August 3rd, 2007, 12:15 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Try this:
Code:
Public Sub ParaStyle()

    ' Start at the beginning
    Selection.HomeKey wdStory

    Dim i       As Integer  ' Keep track of where we are
    Dim MyStyle As String   ' ‘Remember’ styles we’ve seen

    i = 1                                       ' Paragraph we’re in.
    MyStyle = Selection.ParagraphFormat.Style   ' Get the 1st Para’s style

    ' I couldn't find how to figure out the No. of the cur. para. . . .
    ' That’s why I needed to use i, and the Do-Until loop.
    Do Until i = Application.ActiveDocument.Paragraphs.Count

        If Selection.ParagraphFormat.Style = "Heading 3" Then
            If MyStyle <> "Heading 1" And MyStyle <> "Body text" Then
                Selection.ParagraphFormat.Style = "Heading 2"
            End If
        End If

        MyStyle = Selection.ParagraphFormat.Style   ' Store current style
                                                    ' This could be an ‘Else’
                                                    ' in the ‘If,’ above.
        Selection.Move wdParagraph, 1
        i = i + 1

    Loop

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace styles in a document creativeconnect Word VBA 2 June 15th, 2007 07:18 PM
Find&Replace DoctorWho ASP.NET 1.0 and 1.1 Professional 3 June 6th, 2007 09:40 AM
Find and replace? Stuart Stalker SQL Server 2000 8 October 13th, 2005 02:49 AM
Create a find and a find and replace in VB.NET snowy0 VB How-To 0 January 26th, 2004 07:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.