Controlling/Replacing Avaiable Styles

M

ML

We have a document template we are developing that has two sets of styles,
one for numbered headers/section and one for unnumbered.

Is there anyway that we can control which styles are shown to the user based
on say a userform selection where the user picks if they want the numbered
or unnumbered version?

Secondly, is there anyway to replace all the styles throughout a document
from one type to another, so that a macro could possibly be used to change
all the styles from the numbered version to the unnumbered version?
 
J

Jonathan West

ML said:
We have a document template we are developing that has two sets of styles,
one for numbered headers/section and one for unnumbered.

Is there anyway that we can control which styles are shown to the user
based on say a userform selection where the user picks if they want the
numbered or unnumbered version?

This depends on which version of Word you are using. If you are using Word
2003, then this is possible, take a look at the following article. The stuff
on restricted styles is towards the end.

Creating Custom Toolbars for Templates
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=262
Secondly, is there anyway to replace all the styles throughout a document
from one type to another, so that a macro could possibly be used to change
all the styles from the numbered version to the unnumbered version?

If you have another template containing the other form of the styles, then
you can use the OrganizerCopy method to copy styles across.
 
M

ML

This needs to work for Word 2000 and higher.

Is there possibly some way in a macro to replace one style with another
easily?
 
J

Jonathan West

ML said:
This needs to work for Word 2000 and higher.

Then I'm afraid you're stuck, at least in terms of preventing access to
unauthorised styles. The best you can manage is to make it easy for users to
use the authorised styles. The same article I referred you to before will
still help.
Is there possibly some way in a macro to replace one style with another
easily?

You can use the Find object for this. Have the Text and Replacement.Text
properties both set to empty strings, the Format property set to True, the
..Style property set to the old style and the Replacement.Style property set
to the new style.
 
M

ML

Thanks again.
You can use the Find object for this. Have the Text and Replacement.Text
properties both set to empty strings, the Format property set to True, the
.Style property set to the old style and the Replacement.Style property
set to the new style.

So doing is it possible somehow loop through an entire document and reset
the styles on every piece of text? I guess basically I need to do a find
and replace automatically on the entire document, replacing the old style
with the new style.
 
J

Jonathan West

ML said:
Thanks again.


So doing is it possible somehow loop through an entire document and reset
the styles on every piece of text? I guess basically I need to do a find
and replace automatically on the entire document, replacing the old style
with the new style.

Easier would be to do find-replace for each style, searching the entire
document
 
M

ML

Thanks. Do you happen to have a sample of code for doing such a replace on
styles?

This sounds like it should work. Both the numbered and unnumbered styles
are defined in the document so if I can do a macro that replaces A->B then
this should work out.

I have for example "Heading 1 (UN)" and "Heading 1 (N)" and based on the
userform selection want to replace one with the other.
 
M

ML

I think I have it Jonathan.

Sub ReplaceStyles()

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 1
Plain" _
)
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
M

ML

Bit of a problem. When I recorded the macro the replace worked, but when I
play the macro back to do it via the macro it doesn't seem to do the
replace, the old style remains in place unchanged.

Any ideas?
 
K

Klaus Linke

ML said:
Bit of a problem. When I recorded the macro the replace worked, but when I
play the macro back to do it via the macro it doesn't seem to do the
replace, the old style remains in place unchanged.

Any ideas?


Delete the line
Selection.Find.ParagraphFormat.Borders.Shadow = False

In Word2000 (perhaps even 97... not sure) you can set styles to ..Hidden=True, and they won't show in the interface.

Since that's an undocumented feature (as far as I know), that might be risky though... especially for others that have to work with your documents and don't know what you did.
And it's not the same as the feature used by Word2003 (.Visibility=False).

Regards,
Klaus
 

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