RE: How can I modify a style in a macro? by Sammy
Sammy
Wed Jul 23 12:35:02 PDT 2008
Thank you so much for the help, as usual you've gone above and beyond the
call of duty in your reply.
"Jean-Guy Marcil" wrote:
> "Sammy" wrote:
>
> > I'm assuming this will work to turn on bold for example:
>
> Why assume anything? Try it!
>
> > With ActiveDocument.Styles("Body Text").Font
> > .Bold = True
> > End With
>
> In fact, it is almost right. If some of the text to which this style is
> applied in the document already has "manual" bold formatting applied, your
> code will toggle the bold. Meaning that the manually bold parts will become
> "unbold" and the other parts will become bold. If you want to make all text
> bold, regardless of the actual status, you need to remove the manual bold
> formatting first. See my code below.
>
> > But, since numbering sits higher in the Word object model, how would I
>
> Higher? I think you mean "elsewhere."
>
> > modify say the "Aligned At" setting located in the Numbering dialog box? Any
> > help would be greatly appreciated.
>
> Working with list numbering and VBA can be complicated. I suggest that you
> work with list templates. Name the list template the first time you run the
> code (and also using code set all the list parameters). After that, always
> use that name when changing the list template attributes. Naming list
> templates prevents document corruption from list template proliferation when
> we let Word handle them. Altjhough I seem to recall that the later Word
> version (2003 and up) behave better in that regard. Also it gives you 100%
> control.
>
>
> Private Const strStyleName As String = "Body Text"
>
> Sub BoldStyle()
>
> With ActiveDocument.Range.Find
> .Style = strStyleName
> Do While .Execute
> ' This will work, but it will emove ALL manual formatting...
> ' .Parent.Font.Reset
> .Parent.Font.Bold = False
> Loop
> End With
>
> With ActiveDocument.Styles(strStyleName).Font
> .Bold = True
> End With
>
> End Sub
>
> Sub AlignedAtStyle()
>
> Dim ltBody As ListTemplate
> Dim boolAlreadyExists As Boolean
> Const strListName As String = "ListName"
>
> boolAlreadyExists = False
>
> For Each ltBody In ActiveDocument.ListTemplates
> If ltBody.Name = strListName Then
> boolAlreadyExists = True
> Exit For
> End If
> Next
>
> If Not boolAlreadyExists Then
> Set ltBody = ActiveDocument.ListTemplates.Add(True, strListName)
> With ltBody
> With .ListLevels(1)
> .NumberFormat = "%1."
> .TrailingCharacter = wdTrailingTab
> .NumberStyle = wdListNumberStyleArabic
> .NumberPosition = InchesToPoints(0.75)
> .Alignment = wdListLevelAlignLeft
> .TextPosition = InchesToPoints(1.25)
> .TabPosition = InchesToPoints(1.25)
> .ResetOnHigher = True
> .StartAt = 1
> .LinkedStyle = strStyleName
> 'If you were bulding an outline unmbering, you would _
> use this instead:
> '.LinkedStyle = "Heading 1"
> End With
> 'If you were bulding an outline unmbering, you would _
> continue with this:
> ' With .ListLevels(2)
> ' .NumberFormat = "%1."
> ' .TrailingCharacter = wdTrailingTab
> ' .NumberStyle = wdListNumberStyleArabic
> ' .NumberPosition = InchesToPoints(0.5)
> ' .Alignment = wdListLevelAlignLeft
> ' .TextPosition = InchesToPoints(1.25)
> ' .TabPosition = InchesToPoints(1.25)
> ' .ResetOnHigher = True
> ' .StartAt = 1
> ' .LinkedStyle = "Heading 2"
> ' End With
> ' With .ListLevels(3)
> 'etc.
> End With
> Else
> With ltBody
> With .ListLevels(1)
> .NumberPosition = InchesToPoints(0.75)
> .TabPosition = InchesToPoints(1.25)
> .TextPosition = InchesToPoints(1.25)
> .LinkedStyle = strStyleName
> End With
> End With
> End If
>
> End Sub
>
>
> The key here is the line:
> .LinkedStyle = strStyleName
>
> This establishes a link between the style and the list template from within
> the list template definition. If you do it from the style definition instead,
> results are often unpredictable and messy.
>