MS Office Forum / Word / Programming / January 2005
Finding bad formatting
|
|
Thread rating:  |
John Devlon - 29 Jan 2005 16:47 GMT Dear people,
For some time now, I'm struggling to solve a VBA problem.
Some of you already gave some assistance, for which I'm great full.
I'm trying to find some bad formatting in a word document using VBA.
On all paragraphs are paragraph styles applied. On pieces of some paragraphs, are Character styles applied. On other pieces of text, other manual formatting is applied like "bold" or "Bold Italic".
I'm trying to detect the pieces of text that has no "character style" applied and yet have other manual formatting is applied like "Bold Italic".
For example:
Some text is set to bold, this is wrong.
Other text has the character style "BOLD" applied, this is good.
I need to highlight all the bad formatting.
Can anyone please help me?
Thanks
JD
Helmut Weber - 29 Jan 2005 21:54 GMT Hi John, the basic principle seems to be to compare the actual formatting to the formatting defined by the style, as I don't know of a range-property "direct formatting". And there probably isn't such a property.
Dim oChr As Range For Each oChr In ActiveDocument.Characters If oChr.Font.Bold Then If Not oChr.Style.Font.Bold Then oChr.HighlightColorIndex = wdYellow End If End If If oChr.Font.Italic Then If Not oChr.Style.Font.Italic Then oChr.HighlightColorIndex = wdYellow End If End If Next
If this is too slow, because of the size of your docs, or the amount of docs you have to process, then other approaches may be necessary and things might get pretty complicated. One idea would be, to check whole paragraphs first, like if activedocument.paragraphs(1).range.font.bold = 9999999 then ... Which would show, that not all of the paragraph is bold.
If so (not all bold), one could try to check each word in the paragraph, and apply the above algorithm to word-ranges.
If not so (all bold), one could check whether all of the paragraph was formatted directly.
Greetings from Bavaria, Germany
Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
John Devlon - 30 Jan 2005 14:21 GMT Dear Mr. Webber
Allready many thanx for all of your efforts... Your the best
Your first solution works great but is unfortunately to slow, like you mentioned. So like you suggested i'm trying the combination of checking paragraphs and words ... Unfortunately, i'm struggling with some basic operations in word ...
When I'm detecting a "bad" paragraph.... How do i check only that parargraph ?
Now i'm first checking the paragraph and then the letters, but i would like to check the words before the characters...
Any suggestions ...?
Below code does not work due to an error ..
Thanx
Below the script ....
Public Sub CheckDoc() Dim pStyle As Word.Style Dim CheckCurrentPara As Boolean CheckCurrentPara = False
Dim para As Paragraph For Each para In ActiveDocument.Paragraphs Set pStyle = para.Range.Style
'Check bold If para.Range.Bold = 9999999 Then CheckCurrentPara = True ElseIf para.Range.Bold <> pStyle.Font.Bold Then CheckCurrentPara = True End If
'Check Italic If para.Range.Italic = 9999999 Then CheckCurrentPara = True ElseIf para.Range.Italic <> pStyle.Font.Italic Then CheckCurrentPara = True End If
'Check Underline If para.Range.Underline = 9999999 Then CheckCurrentPara = True ElseIf para.Range.Underline <> pStyle.Font.Underline Then CheckCurrentPara = True End If
'This paragraph is not correct If CheckCurrentPara = True Then Dim oChr As Range For Each oChr In ActiveDocument.Paragraphs(para) If oChr.Font.Bold Then If Not oChr.Style.Font.Bold Then oChr.HighlightColorIndex = wdYellow End If End If If oChr.Font.Italic Then If Not oChr.Style.Font.Italic Then oChr.HighlightColorIndex = wdYellow End If End If Next End If
CheckCurrentPara = False Next para
MsgBox "Done"
end sub
############################################################################# ############################################################################# #############################################################################
> Hi John, > the basic principle seems to be to compare [quoted text clipped - 38 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 30 Jan 2005 15:45 GMT Hi John, whatever way, determining whether all kinds of formatting like subscript, superscript, kerning, animation... is direct formatting or formatting via styles would need an enormous lot of time. You have to decide first, what kind of formatting you want to check. Let's say bold, italic, underline and nothing else. Then, I think, this could be a way, e.g., for finding directly as bold formatted parts of the text:
Dim r As Range ' temporary Set r = ActiveDocument.Range With r.Find .Font.Bold = True While .Execute If r.Style.Font.Bold = 0 Then r.HighlightColorIndex = wdYellow End If Wend End With
But this is more complicated, then I thought. I really don't know about all possible complications. Hopefully, you'll never know, too.
Greetings from Bavaria, Germany
Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
John Devlon - 30 Jan 2005 16:24 GMT Dear Mr. Webber
Thank you for all your assistance and good advice. I'm forever in your debt.
Because I only have to check italic, bold and underlined your solution works great ...
Again, many thanks.
Greetings to all good people of Bavaria.
Kind regards,
JD
> Hi John, > whatever way, determining whether all kinds of formatting [quoted text clipped - 28 lines] > Word XP, Win 98 > http://word.mvps.org/ Jezebel - 30 Jan 2005 06:45 GMT There are a couple of methods you can use, depending on the types of bad formatting you commonly need to find. Apart from character-by-character checking for style variations --
1. Check for character styles as such:
For each pChar in MyParagraph.Characters If pChar.Style <> MyParagraph.Style then ... character style applied end if Next
2. Experiment with the Select methods: SelectCurrentFont, Color/Alignment/Tabs, etc ... these methods extend the selection until the respective formatting value changes. So if your entire document should be in 10pt Arial medium, then with the selection at start of document, Selection.SelectCurrentFont will extend the selection until it finds anything that is not 10 pt Arial medium.
> Dear people, > [quoted text clipped - 25 lines] > > JD
|
|
|