Styles Macro

K

Krumrei

I need the ability to take an exsisting Style within a Doc. and have a macro
to change the name of a specific style.

So for instance, I need Bold Header1 to be renamed Bold Header2

I have been looking at the replace feature in VB, but it does not seem to be
working. Here is the code I created to do that.


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Body Text_ACSSC")
.Text = ""
.Replacement.Style = ActiveDocument.Styles("Body Text_ACS")
.Execute Replace:=wdReplaceAll
 
F

fumei via OfficeKB.com

"So for instance, I need Bold Header1 to be renamed Bold Header2"

I am not following. When you say rename, do mean the name of the style? Or,
are you trying to change the style used, from one to another? As in:

Find all instances of Body Text_ACSSC style , and change that text to Body
Text_ACS style

This is easy, but BOTH styles must exist.

You can rename a style with something like:

ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"

This would change "MyNewOne" to "Blah"
 
K

Krumrei

This works ( ActiveDocument.Styles("MyNewOne").NameLocal = "Blah")

BUT, then how would I include Multiple stylechanges within that same string?

So if I have a doc that has to have 10 different names changes, I want to be
able to have them all completed in one shot?\


Thanks for the info!
 
J

Jean-Guy Marcil

Krumrei said:
This works ( ActiveDocument.Styles("MyNewOne").NameLocal = "Blah")

BUT, then how would I include Multiple stylechanges within that same string?

So if I have a doc that has to have 10 different names changes, I want to be
able to have them all completed in one shot?\

You could try something like this:


Dim varOldArray As Variant
Dim varNewArray As Variant
Dim i As Long

Const strOldNames As String = "Name1|Name2|Name3|Name4|Name5|Name6"
Const strNewNames As String =
"NewName1|NewName2|NewName3|NewName4|NewName5|NewName6"

varOldArray = Split(strOldNames, "|")
varNewArray = Split(strNewNames, "|")

For i = 0 To UBound(varOldArray)
ActiveDocument.Styles(varOldArray(i)).NameLocal = varNewArray(i)
Next


But it is not really a "One" shot as you have to run the loop x number of
times.

A true "one" shot is not possible in this case.
It might be easier just to have 10
ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"
consecutive statements.
 
K

Krumrei

Ok,

One step back.....


What I need it to do it change one style to another through the entire
document ( I will have both styles preloaded ) and it has to go through the
header/footer and the body of the document and including table changes in the
styles.

Any suggestions?


Thanks!
 
J

Jean-Guy Marcil

Krumrei said:
Ok,

One step back.....


What I need it to do it change one style to another through the entire
document ( I will have both styles preloaded ) and it has to go through the
header/footer and the body of the document and including table changes in the
styles.

No need of VBA, why don't you simply use the Find/Replace feature in the
Edit menu?
 
J

Jessica Weissman

Are you changing the name of a style, or applying a different existing style
with a similar name to some text?

It sounds more like the latter.

It's tricky to get at the paragraphs in the header and footer. Those
paragraphs are not part of the ActiveDocument.Paras collection. You have to
look at each storyRange in the document.

Good luck. I'm working on something not too dissimilar at the moment
(nothing that would actually be relevant to you).

- Jessica
 
K

Krumrei

Thanks for the references.

One last question:

I have created this code, over and over repeating it for each Stlye in Word
to have the name replaced. I have over 20 lines of code, one for each Style
to be renamed.

However, how do I get it to continue the Macro, if the Name is not within
the style list?
If it is not in there, it just stops the Macro at the point of the unknown
or not listed Style within the document.

Basically, I want the program to skip and go to the next Sub, IF it has not
changed or recognized that the style listed in the document.

Paul


ActiveDocument.Styles("Body Text_ACSSC").NameLocal = "Body Text_ACS"
 
J

Jean-Guy Marcil

Krumrei said:
Thanks for the references.

One last question:

I have created this code, over and over repeating it for each Stlye in Word
to have the name replaced. I have over 20 lines of code, one for each Style
to be renamed.

However, how do I get it to continue the Macro, if the Name is not within
the style list?
If it is not in there, it just stops the Macro at the point of the unknown
or not listed Style within the document.

Basically, I want the program to skip and go to the next Sub, IF it has not
changed or recognized that the style listed in the document.

First, check which of the target custom styles exist, then use that to do
the work.


Dim i As Long
Dim styCheck As Style
Dim strStyles As String
Dim varStyles As Variant

For i = 1 To ActiveDocument.Styles.Count
Set styCheck = ActiveDocument.Styles(i)
Select Case styCheck.NameLocal
Case "Body Text_ACSSC", "Body Text_AC", _
"Title_ACSSC", "Title_AC"
If strStyles = "" Then
strStyles = styCheck.NameLocal
Else
strStyles = strStyles & "|" & styCheck.NameLocal
End If
End Select
Next

If strStyles <> "" Then
varStyles = Split(strStyles, "|")
For i = LBound(varStyles) To UBound(varStyles)
'Do something to each of these styles...
Next
Else
MsgBox "None of the target custom styles were found in this document."
End If
 

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