Results 1 to 5 of 5
  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

    Paragraph styles act like Character styles

    This is very strange (to me).

    I created a paragraph style, (not a character style) called WordBasic2 and ran a program to locate all occurrences of "WordBasic." and apply the style.

    Don't Ask.

    To my surprise, only the found characters "Wordbasic." were styled. I had expected that although only the string "WordBasic." was selected, applying a paragraph style would affect the entire paragraph.

    I tried it manually. Click anywhere in the paragraph (len(selection)=0) and the entire paragraph is styled WordBasic2. Select ten characters (len(selection)=10) and only those ten are styled.

    Years of comprehension of the difference between Paragraph styles and Character styles down the drain, just like that!

    Here's the code:
    <pre> While Selection.Find.Execute
    If strReplaceStyle <> "" Then
    Selection.Style = strReplaceStyle
    Else
    End If
    Selection.TypeText (strReplace)
    Wend
    </pre>


  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Paragraph styles act like Character styles

    Chris,

    I don't know the reason why it occurs, but what you're getting is in line with what I've seen for years - certain styles, when applies to characters and not a full paragraph, will change the style of only those characters. I've never tracked down what causes this to occur sometimes and not other times.

    Just as this dictates the working practice, in the the Word environment, of applying style changes to entire paragraphs rather than just parts, in code you probably need to do something like "Selection.Paragraphs(1).Style = strReplaceStyle".

    Just another one of those Word quirks I guess. <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

    Gary

  3. #3
    JustCallMeAl
    Guest

    Re: Paragraph styles act like Character styles

    This is a "feature" of Word 97 and 2000. If you select part of the paragraph and apply the paragraph style, only that part which you selected will be styled with that style.

    To style the paragraph which contains the words you are looking for, I would throw something like this in:

    <pre>selection.Range.Paragraphs.First.Style = wdStyleHeading1</pre>


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

    Re: Paragraph styles act like Character styles

    > do something like "Selection.Paragraphs(1).Style = strReplaceStyle"

    Thanks, Gary. This is probably a better programming style anyway. "Say what you mean" sort of thing, a good practice which will hold me in good stead once I move away from WordVBA

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

    Re: Paragraph styles act like Character styles

    Thanks, Al, see also my reply to gary. You have BOTH suggested a better standard of coding, and that's always a good direction!

Posting Permissions

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