Results 1 to 5 of 5

Thread: Styles

  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I've noticed that when I had a style programmatically in Word 2007, the style also picks up any attributes that are applied to the text at the cursor position.

    For example, I add a style called "Body Text 18 After". I base the new style on Body Text, and my code sets Space After = 18.

    If the cursor is currently on text that is 24 pt Comic Sans, bold and red color, my new style will have those attributes as well.

    I don't recall that previous versions of Word embraced the attributes at the cursor. This means that if I add several styles programmatically (after determining that they do not exist in the document), I have to write code for every possible attitubte and formatting option to ensure that I'm not getting settings that I don't want.

    Is this the expected behavior of ActiveDocument.Styles.Add ?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    This means that the style is set to update automatically. This feature has been available in all recent versions of Word.

    You can turn it off interactively or using VBA:

    Dim MyStyle As Style
    Set MyStyle = ActiveDocument.Styles.Add
    MyStyle.AutomaticallyUpdate = False

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I get the same results even with AutomaticallyUpdate=False.

    Here's an example: the current paragarph is Normal style, but has been directly formatted with italic and hidden text. If I interactively add a new style, Word will add the default attributes of italic and hidden, making the assumption that I want to mimic the attributes of the current paragraph. That's fine... I see the settings in the interactive dialog box and can easily adjust.

    This same assumption is being made when I add a new style programmatically. None of my styles are set to Automatically Update. If my cursor is in the hypothetical paragraph which I mentioned above, and I add the following code:

    ActiveDocument.Styles.Add "My Style"
    With ActiveDocument.Styles("My Style")
    .AutomaticallyUpdate = False
    .BaseStyle = "Body Text"
    .Font.Size = 16
    End With

    my new style is based on Body Text, 16 pt, AND with italic and hidden.

    The style of the current selection is not changed to My Style, as it would be if I were doing this interactively. In this case, I'm not sure where Automatically Update would come into place.

    Richard

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Oops, I'm sorry. I didn't read your original post carefully enough.

    Word has always behaved this way, as far as I know, both interactively and in VBA. I'm using Word 2002 (aka XP) at the moment, and a new style created in the Styles and Formatting task pane or using ActiveDocument.Styles.Add takes on the formatting of the currently selected text.

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    This behavior for interactive style creation makes sense, but it's annoying with VBA! I guess I'll add a paragraph at the end of the document, apply Normal style, and create new styles with the cursor sitting there. I can delete it when finished. I have not noticed this in previous versions of Word when creating styles with VBA. Could be I never encountered the formatting I am now.

Posting Permissions

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