PDA

View Full Version : ClearParagraphDirectFormatting



Pho3NiX
04-07-2012, 09:42 AM
Hi,
I have two documents, each with it's own style definition.

When I copy text from one document to another the style are adjusted to match source.

Ie: A text with Heading1 style before copy will become Heading1 + Difference in destination

Where Difference is extra formatting needed to make destination Heading1 looks like source Heading1.

I'd like like to remove that extra formatting so Heading1 in source will stay Heading1 in destination (but with destination appearance.)

Something like that does a fair bit of job but will destroy per character formatting like bold / italic that are not applied to whole paragraph.


For each wPara in Selection.Paragraphs
wPara.style = wPara.style
Next

Since word 2007

Selection.ClearParagraphDirectFormatting


is available ans seems to do what I want... but I need a something compatible with word 2003
does any one have any glue on how to reset only paragraph direct formatting ?
or backup only character level formatting to reapply later ?

Thank you very much.

fumei
04-07-2012, 08:42 PM
Sorry, I sure don't, as I never use direct formatting for anything.

This is what you get when you do not use styles the way styles are supposed to be used.

macropod
04-08-2012, 03:55 AM
Try something along the lines of:
Sub Demo()
Dim oSty As Style, StrSty As String, i As Long
Dim StrStyDef As String, StrTmp As String, oPara As Paragraph
With ActiveDocument
For Each oSty In .Styles
If oSty.Type = wdStyleTypeCharacter And oSty.Type <> 1 Then
If oSty.InUse = True Then
StrSty = StrSty & "|" & oSty.NameLocal
End If
End If
Next
End With
For i = 1 To UBound(Split(StrSty, "|"))
With ActiveDocument.Range
With .Find
.ClearFormatting
.Text = ""
.Style = Split(StrSty, "|")(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
StrStyDef = StrStyDef & " " & .Style & vbTab & .Duplicate.Start & "," & .Duplicate.End
.Find.Execute
Loop
End With
Next
With ActiveDocument
For Each oPara In .Paragraphs
oPara.Style = oPara.Style
Next
For i = 1 To UBound(Split(StrStyDef, " "))
StrTmp = Split(StrStyDef, " ")(i)
.Range(Split(Split(StrTmp, vbTab)(1), ",")(0), Split(Split(StrTmp, vbTab)(1), ",")(0)).Style = Split(StrTmp, vbTab)(0)
Next
End With
End Sub

Pho3NiX
04-08-2012, 12:36 PM
Thank you Macropod.

Unfortunately it looks like character styling like applying bold or italic to some part of a paragraph, or unbolding some part of bold text are not stored as NAMED character style in use. Even after saving the document.

The good news is that oPara.style = oPara.Style, does not destroy character styling that are not defined by style. (In my case applying a bigger font to some character stay.)

Thanks also for your comment Fumei,

Unfortunately the content is produced by technical writers who are chosen for other quality than word formatting ability. Then the files are properly formatted by secretary before release. I'm trying to help them in that task, so I have to deal with this half-dirty world on a daily basis. Amongs other things I've spend countless hours trying to make range.InsertFile work in a foolproof way :S

macropod
04-08-2012, 02:41 PM
I thought the objective is to retain only formatting applied by Styles (ie hard-formatting is to be removed). D o you want to do something different?

fumei
04-08-2012, 06:11 PM
Yes Paul...

"Something like that does a fair bit of job but will destroy per character formatting like bold / italic that are not applied to whole paragraph."

"reset only paragraph direct formatting ?
or backup only character level formatting to reapply later ?"

They want to retain individual character formatting (hard-formatting).

macropod
04-08-2012, 06:35 PM
In which case, unless the whole paragraph has hard character formatting:
Sub Demo()
Dim oPara As Paragraph
With ActiveDocument
For Each oPara In .Paragraphs
oPara.Range.Characters.Last.Style = oPara.Style
Next
End With
End Sub

fumei
04-08-2012, 08:28 PM
Really? I will have to try that. It works for all characters that have direct formatting. I would have thought .Last would have applied to the last character. But hey, I will try it. Who knew?