Results 1 to 2 of 2
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Applying non-existent styles (Word97SR2)

    I decided I should soup-up my ApplyStyles macro. Here's (attached) my current effort.

    I have applications that are customised by the client/user/consultant. They, the client/user/consultant, get to nominate a style name to be applied to found text; trouble is - they can type any name into the controlling table and that style might not exist in the document.

    Or in the attached template, or in the Normal.dot. Maybe it exists somehere on the hard drive .....? On the web? In the future?

    YES! That's it! The style doesn't exist yet, but it deserves to exist!


    OK. Start by looking at the macro "TESTApplyStyle". You'll need to create a new document and key in a few paragraphs. TESTApplyStyle will change the style of just the first paragraph.

    The function ApplyStyle controls the hunt, and ultimately applies a style (in my test case, to the first paragraph of the document).

    ApplyStyle makes a cascade of calls to
    <pre> styStyleFromDocument(rng.Parent, strStyleName)
    styStyleFromAttachedTemplate(rng.Parent, strStyleName)
    styStyleFromNormalTemplate(rng.Parent, strStyleName)
    styStyleFromAnyGlobalTemplate(rng.Parent, strStyleName)
    </pre>


    and if none of those succesively penetrating searches succeeds, ApplyStyle uses an existing style (which the developer must supply - after all, EVERY paragraph has a style)
    <pre>Set sty = styAssociated
    sty = styResetStyle(sty, strStyleName)
    </pre>


    styResetStyle is in its infancy. As a separate device I'll write code to analyse a character string (a supplied style name) to see what clues the user has given me.

    For example, if the user has chosen as a name "RedOctober", I'll guess/analyse that they want the colour to be Red; "GreenWithEnvy" as a style name suggest a green colour.

    Likewise "12TimesRedBagels" ought to give me a style whose font colour is Red, typeface is, perhaps, Times New Roman, and whose pointsize is 12pt. Bagels? You're thinking of stale, not style (hah hah!)

    Since ApplyStyle is given a Range as argument, all this ought to work equally well for Character styles as well as Paragraph styles, but I haven't performed exhaustive testing.

    I'm still not sure that I've got the On Error stuff right.


    In the attached text file you should find definitions for the following procedures:
    <pre>Public Function styStyleFromDocument(doc As Document, strStyleName As String) As Style
    Public Function styStyleFromAttachedTemplate(doc As Document, strStyleName As String) As Style
    Public Function styStyleFromNormalTemplate(doc As Document, strStyleName As String) As Style
    Public Function styStyleFromAnyGlobalTemplate(doc As Document, strStyleName As String) As Style
    Public Function styResetStyle(sty As Style, strStyleName As String) As Style
    Public Function ApplyStyle(rng As Range, strStyleName As String, styAssociated As Style)
    </pre>

    Attached Files Attached Files

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Applying non-existent styles (Word97SR2)

    Ooops! Spoke too soon.

    I'd forgotten to ensure that a NEW style object was available; my code of yesterday set "sty" to the associated style, setting only a reference, so that when I reset the characteristics of "sty", i was actually changing the characteristics of the original style.

    The 1-line fix below appears to solve that problem.

    See also my other posts today on various things I've discovered.


    <pre>Public Function ApplyStyle(rng As Range, strStyleName As String, styAssociated As Style)
    ' Procedure : ApplyStyle
    ' Description: Apply a style that may not exist.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: Range, style name, Suggested style.
    ' Returns: None
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By the calls shown below.


    ' We need to apply a named style to a Range; it my not exist.
    ' If the style exists in the document, apply the style.
    ' If the style exists in the attached template, copy and use that style.
    ' If the style exists in the Normal template, copy and use that style.
    ' If the style exists in any Global template, copy and use that style.
    ' Otherwise create the style using the characteristics of the associated style.

    Dim sty As Style
    Set sty = styStyleFromDocument(rng.Parent, strStyleName)
    If sty Is Nothing Then
    Set sty = styStyleFromAttachedTemplate(rng.Parent, strStyleName)
    If sty Is Nothing Then
    Set sty = styStyleFromNormalTemplate(rng.Parent, strStyleName)
    If sty Is Nothing Then
    Set sty = styStyleFromAnyGlobalTemplate(rng.Parent, strStyleName)
    If sty Is Nothing Then
    ' 2002/03/07 forgot to make a new style object!
    Set sty = activedocument.Styles.Add(Name:=strStyleName, Type:=styAssociated.Type)
    sty = styResetStyle(sty, strStyleName)
    Else
    End If
    Else
    End If
    Else
    End If
    Else
    End If
    rng.Style = sty
    'Sub TESTApplyStyle()
    ' Dim rng As Range
    ' Set rng = activedocument.Paragraphs(1).Range
    ' Call ApplyStyle(rng, "RedCaption", rng.Style)
    'End Sub
    End Function
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •