Replacing Styles

M

mmikedm1000

I have two questions concerning using a macro to find styles and replace them
with other styes.

First I am using this code to find and replace a styles
'
' Finds Style "Heading 4" and Replaces it with Style "Heading 3"
'


Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
On Error Resume Next
Selection.Find.Style = ActiveDocument.Styles("Heading 4")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 3")

With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = True
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
I have many styles that are currently in a word document that I want the
macro to find and replace with the style body for instance. Is there a
better way to code this so that maybe five or more different styles can be
replaced with the style body.

My second question is if there is a way to code a find replace to replace an
inline style (maybe wrong terminology) with a defined style.

For example, replacing "body + bold" with "emphasis"

Thanks for any help,
 
B

Bear

Michael:

Here's some information I'd like to put in your head. Just stuff I learned
by trial-and-error and by reading this and other lists.

- You don't have to use the selection to do a find and replace. If you use a
range object instead, you won't change the user's last settings in the Find
and Replace dialog box. (So you don't have to record and reestablish them if
you're being super courteous.)

- You don't have to "position" yourself anywhere in the document when you're
using wdReplaceAll. (The more you use the Word Document Object Model, and
object-oriented programming, the less important the position of the selection
in the document becomes.)

- The odd isolation of some parts of the Find specification outside the With
isn't necessary. It appears when you record a macro, but when you're coding
VBA, you can put the ClearFormatting method inside the With and so on.

- When you use VBA, the Find operation only looks in the main story (the
"body") of the document. You have to do additional work to replace styles (or
anything) in the headers and footers, and in any text boxes.

So your code could look like:

Sub ReplaceStyles()

Dim objRange as Range

Set objRange = ActiveDocument.Range

With objRange.Find
.ClearFormatting
.Replacement.ClearFormatting
If StyleExists("Name1") = True Then
.Style = "Name1"
.Replacement.Style = "NewName1"
.Execute Replace:=wdReplaceAll
End If
If StyleExists("Name2") = True Then
.Style = "Name2"
.Replacement.Style = "NewName2"
.Replacement.ParagraphFormat.SpaceBefore = 0
.Execute Replace:=wdReplaceAll
End If
' Etc., etc.
End With

Set objRange = Nothing

End Sub
 
B

Bear

And... to answer your actual questions as best as I can...

The simplest way to replace multiple styles is probably just to repeat the
Find operation for each style. This is great for a once-only macro, or for an
often-used macro when the styles in question stay the same.

For more flexibility, you could load up an array with the list of style
names you wanted to replace, then loop through each element of the array,
repeating the Replace with each. This might make for less code, and it would
put the target style names in a list at the front of the code, where they'd
be easy to spot and change.

Second question... If the inline (character) style is named, you can use
exactly the same code as for a paragraph style. (Hooray!)

If it's not a style, but manually applied formatting, you can replace that
too. Look in the VBA Help for the various properties and methods of the of
the Find object. (Just open you code, place the insertion point in the .Find,
and press F1.)

Bear
 
M

mmikedm1000

Bear,

Thank you for the information. I edited my macro to the type of replace
that you illustrated. I receive a compile error: sub or function not
defined. It errors on the "StyleExists" code. Any suggestions.

Thanks,

Michael
 
B

Bear

Michael:

I'm terribly sorry. I included a function that I defined and you no doubt
assumed it was a built-in. Here's the function. You can put it in any module
in the same project.

Public Function StyleExists(strStyleName As String) As Boolean

' Determines whether or not the target style exists in the
' active document.

Dim objStyle As Style

StyleExists = False
For Each objStyle In ActiveDocument.Styles
If objStyle.NameLocal = strStyleName Then
StyleExists = True
Exit For
End If
Next objStyle

End Function

Please do get back to me if you have any more problems. I don't want to hurt
more than I'm helping.

Bear
 
M

mmikedm1000

Bear,

You explanations have been most helpful. The macro is working great on
defined styles. I have tried this code to "re-style" body + Bold with a
style emphasis, however it does nothing.

If StyleExists("body + Bold") = True Then
.Style = "body + Bold"
.Replacement.Style = "emphasis"
.Replacement.ParagraphFormat.SpaceBefore = 0
.Execute Replace:=wdReplaceAll
End If

Do you know why? I tried using VBA help for these types of styles, but I
could not find anything. I know that I can open up the styles manually and
select all instances of a styles and than select the new styles. That works
fine, but I was wanting to use a macro, becuase I have may documents to apply
this to.

Thanks for all your help.

Michael
 

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