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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manually changed character styles

Status
Not open for further replies.

harky

Technical User
Oct 29, 2001
39
0
0
GB
Hi

Does any one know how I grab the charater style attributes from the template attached to the open Word document and compare them to the character styles withing the document to determine whether or not they have been manually formatted? eg, I have a character style *xref which is 10pt blue text by default - how can I highlight instances of that style which are say 16pt red which has been manually formatted by the user?

Any help appreciated
Cheers
 
This is a big issue. What EXACTLY do you want to do? Do you want to tell the user something is off? Do you want to actually return instance of "xref" to being just "xref"?

If that is what you want, this is fairly easy. Simply check every word for its character style. If it is xref, then select it and make it xref. Say a user has manually changed the attribute of text that has xref, the code just makes it xref.
Code:
Dim myRange As Word.Range
Dim aWord
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
     aWord.Select
     aWord.Style = "xref"
  End If
Next aWord
In other words it makes it explicitly xref again.

Now if it really is a question of identifying changed attributes, then you CAN do further checking.
Code:
Dim myRange As Word.Range
Dim aWord
Dim msg As String
Dim title As String
Dim response
msg = "This text has been manually formatted.  " & _
    "Do you want to make it the original format?"
title = "Why did you change this?????"
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
    If aWord.Font.Size <> 16 Then
      response = MsgBox(msg, vbYesNo, title)
      If response = vbYes Then
         aWord.Select
         aWord.Style = "xref"
      End If
    End If
  End If
Next aWord
Set myRange = Nothing
So say the font size for xref = 16, you can check to see if:

1. the word IS using the xref style
2. the word font size is NOT 16 (the size set by the style)
3. ask for a decision

Regarding styles, you can check to see if a range has a style, but there is no way to find out if it has been manually changed without actually checking specific attributes. Since there are a LOT of attributes this can be an issue.

Again, exactly what do you want to happen?

Gerry
 
This is a big issue. What EXACTLY do you want to do? Do you want to tell the user something is off? Do you want to actually return instance of "xref" to being just "xref"?

If that is what you want, this is fairly easy. Simply check every word for its character style. If it is xref, then select it and make it xref. Say a user has manually changed the attribute of text that has xref, the code just makes it xref.
Code:
Dim myRange As Word.Range
Dim aWord
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
     aWord.Select
     aWord.Style = "xref"
  End If
Next aWord
In other words it makes it explicitly xref again.

Now if it really is a question of identifying changed attributes, then you CAN do further checking.
Code:
Dim myRange As Word.Range
Dim aWord
Dim msg As String
Dim title As String
Dim response
msg = "This text has been manually formatted.  " & _
    "Do you want to make it the original format?"
title = "Why did you change this?????"
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
    If aWord.Font.Size <> 16 Then
      response = MsgBox(msg, vbYesNo, title)
      If response = vbYes Then
         aWord.Select
         aWord.Style = "xref"
      End If
    End If
  End If
Next aWord
Set myRange = Nothing[/code]So say the font size for xref = 16, you can check to see if:

1. the word IS using the xref style
2. the word font size is NOT 16 (the size set by the style)
3. ask for a decision

Regarding styles, you can check to see if a range has a style, but there is no way to find out if it has been manually changed without actually checking specific attributes. Since there are a LOT of attributes this can be an issue.

Again, exactly what do you want to happen?

Gerry
 
This is a big issue. What EXACTLY do you want to do? Do you want to tell the user something is off? Do you want to actually return instance of "xref" to being just "xref"?

If that is what you want, this is fairly easy. Simply check every word for its character style. If it is xref, then select it and make it xref. Say a user has manually changed the attribute of text that has xref, the code just makes it xref.
Code:
Dim myRange As Word.Range
Dim aWord
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
     aWord.Select
     aWord.Style = "xref"
  End If
Next aWord
In other words it makes it explicitly xref again.

Now if it really is a question of identifying changed attributes, then you CAN do further checking.
Code:
Dim myRange As Word.Range
Dim aWord
Dim msg As String
Dim title As String
Dim response
msg = "This text has been manually formatted.  " & _
    "Do you want to make it the original format?"
title = "Why did you change this?????"
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
    If aWord.Font.Size <> 16 Then
      response = MsgBox(msg, vbYesNo, title)
      If response = vbYes Then
         aWord.Select
         aWord.Style = "xref"
      End If
    End If
  End If
Next aWord
Set myRange = Nothing
So say the font size for xref = 16, you can check to see if:

1. the word IS using the xref style
2. the word font size is NOT 16 (the size set by the style)
3. ask for a decision

Regarding styles, you can check to see if a range has a style, but there is no way to find out if it has been manually changed without actually checking specific attributes. Since there are a LOT of attributes this can be an issue.

Again, exactly what do you want to happen?

Gerry
 
How the heck did THAT happen?????

Gerry
 
Too much....WAAAAAAAY too much. I seem to be have weird DSL connections. Similar things with mail. It comes back stating has not sent...I send again, and end up with multiples.

Hmmmmmm. Sorry about that.

Gerry
 
Hi Gerry

Basically, I need the code to run through all character styles in the file and establish if they have been manually changed. So for xref for example, I would like to know if characters styled xref have been changed to red, or black or 18pt by the editor. So i need to compare the current attributes of a character style against those in its default description and then highlight any instances where they differ. Does that make sense?

Thanks again for your help
Ian
 
Uh....then I suggest you use my code. It does precisely that. OK, it does not do the highlighting, but it could do that easily.

Again, though what EXACTLY, precisely, with no quibbles, do you want to happen???????

You say "highlight"...OK, but what does that mean????? Does that mean every instance where you find a difference, you highlight the text and move on to the next one? Does it mean you highlight it, and stop to query the user? What is exactly the point of highlighting if you do not DO anything about it? So the user can look at the document and go....yup, very nice, YES...that is exactly where I did manually change the format, nice trick to point it out, gosh thanks....COOL.

So sure, you can do what you ask. Please note that you can NOT compare a description of current attribute and the attached style. You can compare explicit attributes. Period. So say xref style = 16 pt, bold = false. Say the user has manually formatted it to 16 pt, bold = false, superscript = true. Unless you actually check for an attribute, explicitly check for it, you do NOT know what it is.

This is why I had the code explicitly reset any found xref style TO the xref style. It wipes out any manual format, and makes it the named style. However, if you need to, you must check EVERY attribute that could be different. If you do not check, there is no way to know if it is different.
Code:
Dim myRange As Word.Range
Dim aWord
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
[COLOR=red]' technically you would have to check
' every single possible attribute text may have
' in order to verify it is NOT different from
' the attribute of the style[/color red]
    If aWord.Font.Size <> 16 _
       Or aWord.Font.Color <> wdBlack _
       Or aWord.Font <> "Arial" Then
         aWord.Select
         Selection.Range.HighlightColorIndex = wdYellow
    End If
  End If
Next aWord
Set myRange = Nothing
You remember that with Or if ANY of the logic statements are true the action will fire.

Again, what exactly do you want to happen?

Gerry
 
Hi Gerry

I want character styled text which has had its attributes manually changed to be highlighted and then find the next instances of manually changed characters style stc throughout the entire document. Then the macro should say to the user "manually formatted character styles have been found, these have been highlighted". Sometimes our editors do search and replaces or select a large group of paras containing character styles and format them - some process that we run on the files fail if character styles have been altered.

So, are you saying it's not possible to create and array that extracts the default description of character styles from the document (or a master doc) and compares them to those of the active document? or is it a matter of hard-coding in all the attributes for character styles (we have about 20 in our data)

Thanks again for your help mate
Ian
 
You must hard code. If you really want to know what the attributes are - for the style AND for the actual text - you have to explicitly check them. Here is a partial list.
Code:
    With Selection.Font
        .Name = "Times New Roman"
        .Size = 12
        .Bold = False
        .Italic = True
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With Selection.ParagraphFormat
        .LeftIndent = InchesToPoints(0)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpace1pt5
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
    End With
    With Selection.ParagraphFormat
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderRight)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 4
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
    End With
End Sub

So, again, say your style did NOT have a shadow border (you never set it), then it is not as if there is an empty, or Null, space/attribute for that. It is explicitly set as False. EVERY attribute is set for EVERY paragraph. There are no Null attributes.

So say, the user made it a shadow border. There is NO WAY to know that (by code anyway) until you actually check.

Are you following? If you need to know if ANY attribute (any at all) is different from the style, you must actually check it. Again, this is why I reset any found instances of a style TO be precisely that style. This makes all the attributes match the style again.

I will ask again. WHAT do you want to really happen? OK, so you go through the document and highlight changed format. OK, you display a message telling the user that changed format has been found, and it is now highlighted.

The user goes....yeah, so what...and does nothing. Is this what you want? If the character style is significant, if it is supposed to be what it is supposed to be...then why are you asking? Why not just change to what it is suppsed to be?
- some process that we run on the files fail if character styles have been altered.
If this is true...don't ask, don't mention it...just change it to the appropriate style.
Code:
Sub Checkxref()
Dim myRange As Word.Range
Dim aWord
Set myRange = ActiveDocument.Range
For Each aWord In myRange.Words
  If aWord.Style = "xref" Then
    If aWord.Font.Size <> 16 _
       Or aWord.Font.Color <> wdBlack _
       Or aWord.Font <> "Arial" Then
         aWord.Select
         Selection.Range.HighlightColorIndex = wdYellow
    End If
  End If
Next aWord
Set myRange = Nothing
Msgbox "Manually formatted character styles have been found, these have been highlighted"
End Sub
There you go. That will go through the document checking for character style "xref", check found instances of xref for specifically Font.Size NOT being 16; Font.Color NOT being black; and Font NOT being Arial.

If you want to check any other attributes you MUST explicitly add them, one by one. There is NO available summary description of style attributes.

IMHO you need to assess purposes. IMHO if a character style has been created then it should have meaning. ANY manual format should revert to the style. in which my simple first code will do it.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top