Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Document Styles 1

Status
Not open for further replies.

OscarPCG

Technical User
Aug 12, 2004
15
US
I would like to search a document for any style that is not on a predefined style list. In the end I want to give the user an opportunity to "correct" the local formatting with a defined style.

I have a hunch that the search & replace feature of Word can get the job done, but I am not sure how to find paragraph styles that are not on a list/array.

Any help will be greatly appreciated.

Oscar
 
It depends on what version of Word you are using.

From 2002 onwards, if you manually format a paragraph it creates a new style, which shows up in the Style dropdown. This style is NOT saved in normal.dot, and therefore is NOT accesible from other documents. it is, however, stored as a local style in the document, if that document is saved.

You can not have a "style" for a paragraph that is NOT on the list.

Even with older versions, if a paragraphs is manually formatted, the style remains the original style. And you can NOT have a paragraph without a style attached to it. All paragraphs have a style, regardless.

Therefore, you have to rethink this. If what you are asking is a check of styles in a document matching a CREATED list, yes you could do that.

You could build an array of styles names.
Check each paragraph to see if there is a match to one of the styles in your array. If not...then do whatever it is you want to do.

You could do a search and replace, but think this one through. Replace with what? A search and replace is explicit. You search for SOMETHING, and you replace with SOMETHING ELSE.

You are talking about a step in between. If you have a number of styles to choose from, how do you know which one to use. You need some context to determine what is the proper style.

So you need to have a dialog with the user to figure out what style to use.

Here is something to possibly get you started. it simply checks for a specific style (Gerry1 - I just made one up for this), and displays a message either yes or no. You would have to expand this, after you figure out exactly what you want to happen. If you have a LOT of styles, you are loop through then all for each paragraph.

Code:
Sub CheckForStyle()
Dim mPara As Paragraph
For Each mPara In ActiveDocument.Paragraphs
    If mPara.Style = "Gerry1" Then
        MsgBox "Yes this is gerry1"
    Else
        MsgBox "Nope this is another style"
    End If
Next
End Sub

Gerry
 
Thanks Gerry

- That was what I needed to go in the right direction.

Oscar
 
On this topic I have an observation to report.
My this code runs fine, when city-name is not actually one of the Styles in the template.

With ActiveDocument.Content.Find
.ClearFormatting
.Style = "city-name"
.Replacement.ClearFormatting
.Replacement.Style = "FontCity"
.Execute Replace:=wdReplaceAll, Format:=True
End With

But this code fails to do anything (without giving any error):
Dim flagFound
flagFound = True
Do While flagFound = True
Selection.HomeKey Unit:=wdStory
Selection.ClearFormatting
With Selection.Find
.Style = "city-name"
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.Style = "FontCity"
Else
flagFound = False
End If
Loop

I am wondering if it's safe to use the first code. Since city-name is not in the list of template Styles, it gives me error when I run it on the template. Any advice ?

TIA,
Sheila

 
You really want this ?
Selection.ClearFormatting

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I am not clear here.
Code:
With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = "city-name"
    .Replacement.ClearFormatting
    .Replacement.Style = "FontCity"
    .Execute Replace:=wdReplaceAll, Format:=True
End With

your first code, give me an error. You state it runs fine. It fails for me, as there is no "city-name" style in my document.

Could you clarify.

Gerry
See my Paintings and Sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top