![]() |
|
#1
|
|||
|
|||
![]()
I've inherited a document (200+ pages) which I have to clean up.
The document has many styles of the form <style name> + condensed <number> The following code doesn't normalise these styles and I can't seem to delete them manually. They go but then come back again. I've turned off the sub-pixelling option for screen display. But now I'm stuck Any help would be appreciated. Code:
Sub NoCondensedandStuff() Dim mystyle As Style For Each mystyle In ActiveDocument.Styles Debug.Print "Processing " & mystyle.NameLocal With mystyle.Font .Spacing = 0 .Scaling = 100 .Position = 0 .Kerning = 0 End With ActiveDocument.UndoClear Next End Sub |
#2
|
||||
|
||||
![]()
Changing the Style definitions won't delete Styles named in the form <style name> + condensed <number>. You would have to delete those Styles. To do that, though, you'd probably want to reapply each <style name> without the 'condensed' part to all affected content in the document; otherwise deleting the Styles named <style name> + condensed <number> would see all their content revert to the Normal Style.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Reapply the basestyle is the clue I needed. I feel a macro coming on.
Many thanks again |
#4
|
|||
|
|||
![]()
This isn't as easy as I tought.
If I go to the style pane I will find lots of sequences like <style name> <style name> + condensed <number1> <style name> + condensed <number2> <style name> + condensed <number3> <style name> + condensed <number4> <style name> + condensed <number5> <style name> + condensed <number6> etc If I select all instances of one of the lines above (from the style pane) it is individual characters that are affected. If I the use the font tab on the ribbon to reset the condensed setting back to normal, that particular style dissappears from the task pane. It seems I was deleting but as there are lits it didn't seem like they were diminishing. To write a macro I either have to be able to acces the style pane facility for selecting al instances of a style or search through the document on a character by character basis. Its not clear why my code above doesn't work when emulating the same operation from the style pane does work. Condensed is the example I'm using . I also have lots and lots of expanded, left, right, raised, lowered, in multifarious combinations so finding a macro solution would be great. All of this stuff is unecessary extraneous formatting which can be safely removed. Any more thoughts. |
#5
|
|||
|
|||
![]() Quote:
Code:
Sub reset_FP() With ActiveDocument.Range .Font.Reset .Paragraphs.Reset End With End Sub |
#6
|
|||
|
|||
![]()
Thanks for the suggestion. However because of a design decision some of the styles affected rely on user applied local formatting. e.g. we only have one style for tables which is 'Table text'. Users a directed by the style guide to use local formatting such a bold for heading text or to center /right align text within tables. Consequently your code can't be used as I wish to retain correct local formatting but get rid of unwanted formatting.
The unwanted formatting is easy to identify by a comparison against the base style. BUT the only way I worked out to do this is on a character by character basis. This is because when you use the style pane you can select all instances of the particular style and when I do this I can see that in many cases there are only individual characters or words affected. Sometimes only 1 instance, sometimes many hundreds of instances. I have a strong suspicion that some of the underlying problems are due to the fact that parts of the final document came from sections authored in Japan. I'll keep scratching my head. But other suggestions are welcome. |
#7
|
||||
|
||||
![]()
Try:
Code:
Sub DeleteCondensedStyles() Dim Doc As Document, i As Long, Rng As Range, StlNmFnd As String, StlNmRep As String Application.ScreenUpdating = False Set Doc = ActiveDocument With Doc For i = .Styles.Count To 1 Step -1 With .Styles(i) If .BuiltIn = False Then If .Type = wdStyleTypeCharacter Then StlNmFnd = .NameLocal If InStr(StlNmFnd, "+") > 0 Then StlNmRep = Trim(Split(StlNmFnd, "+")(0)) For Each Rng In Doc.StoryRanges With Rng.Find .ClearFormatting .Format = True .Style = StlNmFnd .Replacement.Style = StlNmRep .Execute Replace:=wdReplaceAll End With Next If .Linked = True Then Doc.Styles.Add Name:="TmpSty" .LinkStyle = "TmpSty" Doc.Styles("TmpSty").Delete Else .Delete End If End If End If End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]()
Macropod
Thanks for the suggestion. Unfortunately when iterating through the style collection the troublesome styles are not found. i.e. the following code prints nothing. Code:
Sub findplus() Dim myindex As Long For myindex = 1 To ActiveDocument.Styles.Count If InStr(ActiveDocument.Styles(myindex).NameLocal, "+") > 0 Then Debug.Print ActiveDocument.Styles(myindex).NameLocal End If Next End Sub |
#9
|
|||
|
|||
![]()
Macropod, If it helps I can send an anonymized document for you to have a good laugh with.([a-zA-X replaced with x and inlineshsapes replaced by a text statement)
|
#10
|
||||
|
||||
![]()
OK, if you attach said document to a post, I'll take a look at it. You can do that via the paperclip symbol on the 'Advanced' posting menu.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
Macropod. Much appreciated. I'd really like to find a programmatic way of resolving this conundrum. The file is uploaded as a zip file as it is too large to upload as docx.
The file is anonymised by replacing all characters with x and replacing inlineshapes with a text statement. Since creating the anonymised version I've resolved the issue with heading numbering by creating a new document from the template and importing the old document into the new document. The 'design philosophy is that the user only sees a very short list of recommended styles (14 at the minimum) but depending on the task they are doing they can add in other sets of styles through a customised ribbon. That bit will be missing from the anonymised document. Thanks again |
#12
|
||||
|
||||
![]()
Your document has far more going on than just 'condensed' Styles; in fact, it has about 250 custom Styles with micro-variations on the base styles. The simplest way of cleaning up this mess is to:
1. Create a new 'base' document containing just the Styles you want to use; 2. Save the existing document in RTF format 3. Open the document in WordPad, make an edit somewhere, then re-save & close. 4. Re-open the edited RTF document in Word and copy and paste its content into the new 'base' document as formatted text; 5. Re-apply the required Styles.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
Macropd. Thanks for the advice. Horrible isn't it. It never ceases to amaze me what people get upto with word. The previous document I was working on had floating text boxes as headers (not in the heading) and diagrams where text had been laid out with newpaper columns and then floating text boxes drawn around the tabbed text!!!!
|
#14
|
||||
|
||||
![]()
That suggests the previous version might have at some stage been in PDF or print format and a low-end OCR package was used to convert it to a Word document. Such horrid outcomes are typical of low-end OCR packages.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
![]() Quote:
After some headscratching I came up with the following code Code:
Sub ConsolidateStyleVariations() Dim myStyle As Style Dim myRange As Range Dim myChar As Range Dim myPage As Long Application.ScreenUpdating = False myPage = 0 For Each myChar In ActiveDocument.StoryRanges(wdMainTextStory).Characters Set myStyle = myChar.Style If myPage + 5 < myChar.Information(wdActiveEndPageNumber) Then DoEvents myPage = myPage + 5 End If If myChar.Fields.Count = 0 Then myChar.Font.Spacing = 0 myChar.Font.Scaling = 100 myChar.Font.Name = myStyle.Font.Name If Not myChar.Font.Position = 0 Then If myChar.Font.Position > 0 Then myChar.Font.Position = 0 myChar.Font.Superscript = True End If If myChar.Font.Position < 0 Then myChar.Font.Position = 0 myChar.Font.Subscript = True End If End If If myStyle.Type = wdStyleTypeParagraph Then myChar.ParagraphFormat.FirstLineIndent = myStyle.ParagraphFormat.FirstLineIndent myChar.ParagraphFormat.LeftIndent = myStyle.ParagraphFormat.LeftIndent End If End If Next Application.ScreenUpdating = True End Sub a. I dont have any styles in my document with changed spacing,position or scaling b. There are very very few occasions where firstline and left indents differ from the style so I'm prepared to take the hit. There were a couple of issues • The fields statement doesn't capture drawing boxes. WHen I checked I only had 4 such items, 3 of which did not convert to inline shape. So when the macro fell over I just set it to continue at the next statement • It takes a loong time to run, hence the inclusion of do events • It took 13 hours to run before I added the screen updating commands The code works because for dynamically created styles (which was creating the issue) if you reset those style to remove the items after the + then this allows word to obligingly remove the dynamic style. Any comments on adding code to trap drawing boxes or make it more robust/faster would be most welcome. For now I'm a lot happier as I have a document that is editable (I don't have to wait 5-10 seconds for the document to update every time I make an edit and it is a programmatic solution so can be added to the suite of 'Triage' macros I'm assembling. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
-SLIME- | Word | 1 | 04-09-2016 08:51 AM |
![]() |
Halesowenmum | Project | 1 | 10-06-2014 06:18 PM |
![]() |
Red Pill | Word | 5 | 05-25-2012 01:06 PM |
Can't Display "Recently Used Styles" in Styles Pane | mwildem | Word | 2 | 05-23-2012 01:42 PM |
![]() |
Pedro77 | Word | 3 | 10-15-2011 05:17 AM |