PDA

View Full Version : Change names of derivate styles



saban
10-22-2007, 06:58 AM
I need some help

I have a document which has tons of styles in it. The problems are the one that are modified addtionaly

(ex. I have style Added (which is normal text no bold no italic) and then someone selects a word in Added style and decides to bold it and now I have Added+bold Style created) What I would like is:

1. to find those styles that are somesort of child objects to original style
2. And change their templates so they are based on Normal style not Added->which I created

3. And then keep the formating that was before changing it from added to normal


Actually I would like all the styles that have + under styles and formating to became separate styles and not derivates from already exisiting styles

fumei
10-22-2007, 10:07 AM
Sorry, but this is an educational issue primarily. You need to teach your users to not make manual format changes.

I don't know about 2003, maybe it is different, but in 2002 those styleName + yadda are display only. While they persist in the document - if you save the file and open it again those "+" styles are still listed - they are NOT real style objects.

Word parses the document and where ever it finds a paragraph with a manually modified style, it listed that with the underlying style, and the "+" in the Syles dropdown.

However, look to the right side of the dropdown.

There is no pilcrow, or character symbol, indicating it is a particular style. Further, if you open the Styles and Formatting Task pane, and click modify, the name of the "style" in the Change Style dialog is "Style styleName + yadda"...not "style + yadda"

Next:Sub CheckStyles()
Dim aStyle As Style
Dim bolFoundOne As Boolean
Dim msg As String
For Each aStyle In ActiveDocument.Styles
If InStr(1, aStyle.NameLocal, "+") > 0 Then
bolFoundOne = True
msg = msg & "Style name with + found. " & _
aStyle.NameLocal & vbCrLf
End If
Next
If bolFoundOne = True Then
MsgBox msg
Else
MsgBox "No style with a + found."
End If
End SubYou can run through all the Styles in the document, and you could a hundred styleName + yadda listed, but you will never find one.

Say you put the Selection on a changed paragraph - listed as styleName + yadda. Run:Sub CurrentStyle()
MsgBox Selection.Range.Style.NameLocal
End SubIt will return: "styleName"

It will NOT return: "styleName + yadda"

Word figures out manual differences, and lists them. However, they are NOT real styles. Therefore, finding them programatically is difficult. You have to test them for every possible difference. There are a LOT of possible differences.

Actually I would like all the styles that have + under styles and formating to became separate styles and not derivates from already exisiting styles Unless you explicitly make a style "Based on" = "no style", then ALL styles are derivatives of an existing style. Mostly often, Normal style.

fumei
10-22-2007, 10:51 AM
There IS a way to (sort of) fix your problem, but unfortunately it is restricted to paragraphs styles. And it looks like your users are making virtual character styles.

That is, they are selecting parts of a paragraph and manually formatting. This is very difficult to fix.

But....if they are changing paragraph styles, then you can do what you are asking.Dim myDupFont As Font
Dim NewName As String
Set myDupFont = Selection.Font.Duplicate
NewName = InputBox("New style name?")

ActiveDocument.Styles.Add Name:=NewName, Type:= _
wdStyleTypeParagraph
ActiveDocument.Styles(NewName).Font = myDupFont
Selection.Style = NewName

1. Open the Style and Formatting Task Pane.
2. Right click one of the styleName + yadda "styles"
3. Select "Select all x instances"

Now run the code above.

What it does:

1. Duplicates the format to a Font object
2. Asks for an name with an Inputbox
3. Create a new style with that name
4. Applies that style back to the Selection.

Thus if you have 17 instances of "MyStyle1 + yadda", you can;

1. use the Style and Formatting Task Pane to "Select all 17 instances"
2. input a name - say "MyStyle1Bold"

All 17 instances of "MyStyle1 + yadda" will now be "MyStyleBold"......AND...."MyStyle1 + yadda" will disappear from the listing. Because there ARE no more instances of it, and Word will not list it.

You can not programatically do this. You have to manually use the Task Pane to "Select all x instances" of a + "style", THEN run the code.

Demo attached.

There are three macros, fired by text "buttons", on the top toolbar.

"Check for +"
"Current style"
"Fix it"

Open the Task Pane, and switch to Styles and Formatting.

Notice there are some "+ styles" listed.

Click "Check for +". You get the message "No style with + found."

Put the cursor into a "+" style paragraph. Click "Current style". It is display the underlying style, NOT the + yadda.

OK. Now, right click MyNew1 + Bold in the Task Pane, and select "Select all 2 instances."

Click "Fix it". Enter a name - say, MyNew1Bold. Press Enter (or for you weird have-to-use-the-mouse people, click OK).

Voila. All instances of the "+" style are made into the new style you named, AND the + style is removed...as there are no more instances of it.

NOTE AGAIN: this will not work for manually formatted character styles. Quite another ball game.

saban
10-23-2007, 05:04 AM
Thnx guys very much for this answers I really appreciate this

fionabolt
10-23-2007, 05:41 AM
Saban

If I have understood your query correctly, you want to re-set all the styles in a document back to the base style, then try this:

Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
p.Range.Style = ActiveDocument.Styles(wdStyleDefaultParagraphFont)
Next p


This will reset the style back to the actual settings of the style. Note that the style of a paragraph is determined by the first character of the paragraph. Therefore , if you have multiple styles within paragraphs then you would have to adapt this code to loop through each instance of a style, turn that into a range and then set the range style back to the wdStyleDefaultParagraphFont.

Regards,
Fiona

saban
10-23-2007, 07:24 AM
But I also need to keep the formatting of text, not just reset it

It is hard to explain but that it is some workaround because some other program which interacts with word does not digest words styles well :)

The perfect solution would be :

1. find all styles that have + signs in it (but as mentioned before it i shard because those styles are not recognised as styles in VBA

2. And then modify and rename them so they are based on "normal" style and the name can be random (like custom1, custom2 etc.. instead of added + bold+ notitalic)

I have found a solution that might help but it has nothing to do with styles
1. select all bold fonts reset them (ctrl+spacebar) and put them back to bold, and same would I do for italic, normal and bold+italic text

Maybe then I would at least get rid of that additional styles which cause a problem

Thnx for suggestions