Replace All Normal Style With New Style

C

Carjoy

Clients of ours can create what is called "string documents". This is a
document that uses the PRIVATE field and VBA code to append all the documents
listed into one document. All the documents use the Normal style with manual
formatting so you can guess what happens when they are appended into one
document. Things become a mess when one document is Times 11 and the other
is Time 12 (and it can't change from that). Historically they never used
styles -- ever. Being new the company, the problem of helping the clients
retain the formatting used in each separate document after they are combined
falls to me. Use Master and Subdocuments are not an option.

What I want to do via VBA is search the document for anything that is using
Normal and create a new style based on (No Style) and call it the 8 character
file name. All files are guaranted to have a 8 character name. Plus if
there is manual paragraph formatting such as center or justified, I want to
create an additional style called FileName2, FileName3.

Am I dreaming? I've found several examples of searching the document for
styles and changing the base font but not one that creates a style on the fly
based on the fomatting of the selected paragraph.
 
W

Word Heretic

G'day Carjoy <[email protected]>,

Essentially yes, you are dreaming. Word provides styles so you can
control formatting. No styles, no control.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Carjoy reckoned:
 
C

Chuck

You're not dreaming -- your nightmare is real.

You could conceivably do what you want with styles but creating loads of new
styles may not be the most efficient way of handling this situation.

My understanding of what you're saying is that when the various docs are
appended the text of each doc has to retain its "native" (own) formatting and
not take on the Normal style formatting of the doc it's being appended to.
So if the Normal style of doc1 is 11pt and the Normal style of doc2 is 12pt,
when you append doc2 to doc1, the text currently in doc1 stays 11pt and the
text currently in doc2 stays 12pt.

You could loop through doc2 before appending, and for each paragraph using
Normal style, apply or create a new style that uses the current document name
(or a variation thereof) and the formatting applied to that paragraph -- see
code below for a general example of how to do that.

However, given all the possible formatting permutations involved with style
vs manual formatting, you could end up with load and loads of new styles that
aren't named in any informative way and so aren't really helpful.

Instead, before appending, you might loop through each Normal styled
paragraph, check whether formatting options have been manually changed from
the style definition and if not, manually apply them to the paragraph. So if
Normal.Font.Size is 10 and the paragraph font size is 11, you'd leave the
formatting alone, but if the Normal style Font.Size is 10 and the paragraph
font size is 10, you'd manually apply Font.Size 10 so that when the text is
appended to the main document the manual formatting travels with it (you
might need to first check what the document's Normal style Font.Size setting
is, store that as a variable then change the Normal style Font.Size to
something completely unlikely and then loop through applying the Font.Size
stored as a variable to any paragraphs where the Normal style Font.Size =
paragraph Font.Size).

And so on. Realistically you'd need to focus your efforts on a few
formatting options like font size, font name and paragraph alignment.

If this is a long term project as opposed to a one off, personally I'd
explain to the client why what they're trying to do is unreasonable,
emphasizing the hidden costs in time and wasted effort, and providing them
with an alternative way of doing things that achieves their goals without
making everyone nuts. Such as providing them with co-ordinated templates and
styles, with toolbars and shortcut keys, as well as training. An added bonus
of getting the client to mend their ways is that it would generate extra
income for you/your employer through implementing the new improved system.

Good luck.

Code for creating new styles (doesn't include code for setting formatting):

Sub StyleCreator()

Dim parPara As Paragraph
Dim styStyle As Style
Dim strFileName As String
Dim strStyleName As String
Dim x As Long
Dim blnFound As Boolean

strFileName = ActiveDocument.Name

'strip .doc from filename
If Right(strFileName, 4) = ".doc" Then
strStyleName = _
Left(strFileName, _
Len(strFileName) - 4)
Else
strStyleName = strFileName
End If

For Each parPara In ActiveDocument.Paragraphs

If parPara.Style = "Normal" Then
GoSub CreateStyle
parPara.Style = strStyleName
End If

Next parPara

Exit Sub

CreateStyle:

With ActiveDocument

x = 1

For Each styStyle In .Styles
If styStyle.NameLocal = strStyleName Then
blnFound = True
End If
If blnFound = True Then
x = x + 1
End If
Next styStyle

If x = 1 Then 'style doesn't exist already
strStyleName = strStyleName
Else
strStyleName = strStyleName & x
End If

.Styles.Add _
Name:=strStyleName, _
Type:=wdStyleTypeParagraph

With .Styles(strStyleName)
'define style here
End With

End With

Return

End Sub
 
J

jb

Carjoy said:
Clients of ours can create what is called "string documents". This is a
document that uses the PRIVATE field and VBA code to append all the documents
listed into one document. All the documents use the Normal style with manual
formatting so you can guess what happens when they are appended into one
document. Things become a mess when one document is Times 11 and the other
is Time 12 (and it can't change from that). Historically they never used
styles -- ever. Being new the company, the problem of helping the clients
retain the formatting used in each separate document after they are combined
falls to me. Use Master and Subdocuments are not an option.

What I want to do via VBA is search the document for anything that is using
Normal and create a new style based on (No Style) and call it the 8 character
file name. All files are guaranted to have a 8 character name. Plus if
there is manual paragraph formatting such as center or justified, I want to
create an additional style called FileName2, FileName3.

Am I dreaming? I've found several examples of searching the document for
styles and changing the base font but not one that creates a style on the fly
based on the fomatting of the selected paragraph.
HI Caren,

I think it is possible to do this although it is a lot of un-necessary work.

What you'd need to do is get the current formatting of the selected
paragraph and store the info for adding to the new style you'd create in
the main document in an array (for all the related style info).

What I'd do here though is get a hold of the company by the short and
curlies and make sure they use a standard template for this "string
document" useage.

HTH
J
 
C

Carjoy

Thanks for your help. I decided to go a different route. Instead of
creating a style based on the name of the document, I just applied ALL
FORMATTING manually. Thus when the document is appended to a new document,
manual formatting overrides the style. While I'm very aware this is not the
preferred method of treating Word documents, this gives me an immediate
solution for an unhappy client and gives me time to start the Styles
education process.

Without going into specifics, Word documents are used as the output for a
Visual Basic application my company sells. The software has been around for
15 years and the clientele is not one that cares about the finer points of
using Word. They just want to print the results of their database entries
and move on (or so I'm told). The term "we've always done it this way" is
mentioned to me a dozen times a week.

I'm going to consider it my greatest challenge yet to change the mind set of
this industry and press on. Never say never.

Many thanks

CarJoy
 
C

Charles Kenyon

This is probably not a good solution either. All text in a Word document has
a style applied to it. When you apply manual formatting, you start with that
base, say TNR, 12 pt. You make your text bold by applying direct formatting.
The document into which you paste your text has the same style, but for some
reason it has Arial 9pt bold. You end up with Arial 9pt text _not bold_ when
you paste.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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