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 13th, 2007, 07:30 PM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replace styles in a document

I need some code to replace one set of paragraph and character styles for another in a document. I have written a module using Find and Replace but it is very cumbersome, and I'm sure you guys will be able to tell me a neater way. It just needs to replace e.g. "main heading" with "heading 1" etc. and then I need to delete the old customised styles to tidy up.

 
Old June 14th, 2007, 12:02 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Here is the way of iterating though paragraphs

Sub Swap_styles()

For Each Para In ActiveDocument.Paragraphs

    Select Case Para.Style

        Case "Normal"

            'Do nothing

        Case "Heading 1"

              Para.Style = "H1"

        Case Else



    End Select
Next Para
End Sub

Find should be faster if your styles to be replaced are less

Cheers
Shasur

http://www.vbadud.blogspot.com
 
Old June 15th, 2007, 07:18 PM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks - that's very concise. There's just one thing I've noticed, however, that if I use it where some words in the paragraph have additional character formatting (e.g. style appears in style box as "normal + bold"), the main style is ignored, whereas the Find and Replace seems to deal with it OK, subsituting the style and leaving the extra formatting in place, which is what I need.
Wendy






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with styles toddw607 Javascript 3 June 3rd, 2008 12:37 PM
Conditional Find and Replace Styles creativeconnect Word VBA 1 August 3rd, 2007 12:15 PM
Chart Styles lumaar BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 June 8th, 2006 01:05 AM
Styles tsimsha HTML Code Clinic 2 February 9th, 2006 11:32 AM
Styles problem tsimsha HTML Code Clinic 9 November 17th, 2004 09:58 AM





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