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

Renaming styles in a word document 1

Status
Not open for further replies.

amillia

Programmer
Nov 14, 2001
124
US
What would be the best way to rename styles in a word document? Could I use a search and replace procedure?
 
Okay, now I am confused. Let me start from the beginning. I tend not to work on a need to know basis when here you need to know everything.

I have over 1200 very long word documents with up to 18000 pages per document. All without any styles. These documents are being converted to XML with a software that requires the documents to have word styles in order to properly map the tags in XML to the styles in the word documents. I have about 500 named tags in the XML DTD that I will use frequently. So I will have 500 named styles in the word documents. The documents all comply to a standard so styles should be consistent. My plan is to name the styles by opening the document selecting the text that contains the correct font, indent and blah blah blah, hitting record macro, then naming the style. This is how I plan to create the style and I will be naming them so the names of the style match or resemble the name of the tag I will later be mapping in XML. Okay so I know how to create the styles. Now how do I apply them to the word documents? With your help my idea is to loop through the document using the paragraph object, then If the text has all of the attributes of the selected style then apply that style to the paragraph.

Does this make sense?

Will using the paragraph object loop through the titles and tables in the document or just the paragraphs?

What code window?

thanks lauren
 
Oh my lordy lordy lordy. You have got to be ( expletive deleted) kidding! 1200 documents with up to 18000 pages, all manually formatted.

First off, whoever was involved with this - and there must be more than one - should be fired....or shot. That is unforgiveable, to be the most charitable I can come up with.

How can you POSSIBLY be sure the documents comply with a standard. If it was a real standard, then styles would have been used. Really, how can you possible know the documents comply? Have you personally checked each page? I hardly think so.

OK, you have a problem. Let's deal with housekeeping first.

The code window here at Tek-Tips is easy. Just put
Code:
 at the beginning of your code, and
at the end. You may want to take a look at the Process TGML link at the bottom of the editor.

The paragraph object will loop through all paragraphs, and a title is just another paragraph.

Tables are another issue. It can be done though.

Back to your problem. You are being inconsistent yourself. You state that the documents comply to a standard. If that is true (and my experience believes this as much as green cheese on the moon), why are you opening each document to check styles. In theory you could open ONE and create the styles, then open the others to apply the styles.

HAHAHAHAHAHAHAHAHAHAHAHAHA....not gonna happen.

HOWEVER, you do in fact have the idea. And I am sorry to say that there is probably no way around it. You will have to check the format of each paragraph - with all the possible properties - then check against

....wait a minute, I may have a way to improve the speed up. Because it is gonna take a looooooooonnnngggggg time to do this.

Start by testing your creation of styles for this.

I will be back. I am currently out of town, and dialing in at 56K....it seems so slow. wait a sec...it IS slow.

Gerry
 
Oh crap, I forgot that putting the text for the code window puts it IN a code window.

Uh, put the word "code" between square brackets at the start of the code, and the word "/code" between square brackets at the end.

Lauren, this is not particularly tricky VBA, but you may want to post this at the Word forum at vbaexpress.com. It is free to join. I live there as well, and you will be able to private message me. I will help you.

Gerry
 
To get
Code:
my code here
type [ignore]
Code:
my code here
[/ignore]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I tried to enter vb express but I can't post new threads. My name there is prestodb. I will try the chat there for the next few days around 3:30 eastern time.

Okay, as for this problem with the vba I am not too concerned about missing styles in the documents because the tech writers can add tags in XML after the import. My objective is get 50 and 75%. Of course the more the better. We went through and identified 43 main styles and I have created those with macros. So when I did this all of the paragraph formats and fonts and basically the stuff you mentioned earlier in the previous post on Oct 15 was created. Okay, so now I am working on the most efficient way to take this information
Code:
Sub Macro_Alpha_Tle()
'
' Macro_Alpha_Tle Macro
' Macro recorded 10/18/2005 by CIS Department
'
    ActiveDocument.Styles.Add Name:="Alpha_title", Type:=wdStyleTypeParagraph
    ActiveDocument.Styles("Alpha_title").AutomaticallyUpdate = False
    With ActiveDocument.Styles("Alpha_title").Font
        .Name = "Arial"
        .Size = 10
        .Bold = True
        .Italic = False
        .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
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With ActiveDocument.Styles("Alpha_title").ParagraphFormat
        .LeftIndent = InchesToPoints(0)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(0.98)
        .Alignment = wdAlignParagraphCenter
        .WidowControl = False
        .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
    ActiveDocument.Styles("Alpha_title").NoSpaceBetweenParagraphsOfSameStyle = _
         False
    ActiveDocument.Styles("Alpha_title").ParagraphFormat.TabStops.ClearAll
    ActiveDocument.Styles("Alpha_title").ParagraphFormat.TabStops.Add Position _
        :=InchesToPoints(5.8), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderDots
    With ActiveDocument.Styles("Alpha_title").ParagraphFormat
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorAutomatic
        End With
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 4
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    ActiveDocument.Styles("Alpha_title").LanguageID = wdEnglishUS
    ActiveDocument.Styles("Alpha_title").NoProofing = False
    ActiveDocument.Styles("Alpha_title").Frame.Delete
End Sub

and apply it to as much of the text as possible. I have looked at different options and concluded that I am going to have to type every one of these attributes in the if statement. So something like this

Code:
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument.Range.Paragraphs()
    If oPara.Range.Font.Name = "Arial" And oPara.Range.Font.Bold = False And oPara.Range.Font.Italic = False And oPara.Range.Font.Underline = wdUnderlineNone And oPara.Range.Font.UnderlineColor = wdColorAutomatic And oPara.Range.Font.StrikeThrough = False And oPara.Range.Font.DoubleStrikeThrough = False And oPara.Range.Font.Outline = False And oPara.Range.Font.Emboss = False And oPara.Range.Font.Shadow = False And oPara.Range.Font.Hidden = False And oPara.Range.Font.SmallCaps = False And oPara.Range.Font.AllCaps = False And oPara.Range.Borders.DistanceFromTop = 1 And oPara.Range.Borders.DistanceFromBottom = 1 And oPara.Range.Borders.DistanceFromLeft = 4 And oPara.Range.Borders.DistanceFromRight = 4 And oPara.Range.Borders.DistanceFromLeft = 4 And oPara.Range.Borders.Shadow = False And oPara.Range.Shading.BackgroundPatternColor = wdColorAutomatic And oPara.Range.Shading.ForegroundPatternColor = wdColorAutomatic
oPara.Range.ParagraphFormat.LeftIndent = InchesToPoints(0.5) And oPara.Range.ParagraphFormat.RightIndent = InchesToPoints(0.5) And oPara.Range.ParagraphFormat.SpaceBefore = 0 And oPara.Range.ParagraphFormat.SpaceBeforeAuto = False And oPara.Range.ParagraphFormat.SpaceAfter = 0 And oPara.Range.ParagraphFormat.SpaceAfterAuto = False
    
 Then       oPara.Range.Style = "Alpha_style"
    End If
Next

I still don't think that this is the most efficient way to write the if statement. I mean all of the attributes are already defined in the style, why should i have to state them again. Beside the fact that I am not even able to put this stuff in a list I have to put it all on one line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top