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

    What style is that? (Word97SR2)

    From the VBA help files for "Style", propery : "When you return the style for a range that includes more than one style, only the first character or paragraph style is returned."


    The above paragraph of text is formatted in paragraph style csA
    Letters "b" are formatted in character style csB
    Letters "c" are formatted in character style csC
    Cursor in letters "a" returns csA, csA
    Cursor in letters "b" returns csB, csA (character style shows first)
    Cursor in letters "c" returns csC, csA (character style shows first)
    Cursor in letters "b" and "c" returns csA, csA
    Cursor in letters "c" and "b" returns csA, csA

    <pre>Sub testSetStyle()
    Dim rng As Range
    Set rng = Selection.Range
    MsgBox rng.Style
    MsgBox rng.Paragraphs(1).Style
    End Sub

    Theories (to date): if you have used .Find to locate a chunk of text and want to know what styles are involved:

    1) you will always be able to determine the Paragraph style; there can be only one paragraph style assigned to a paragraph.

    2) if the found range spans two or more character styles, you'll get the same result (from .Style) as if there were no character formatting.

    3) the only way to determine if one or more character formats are assigned is to examine each character in the found range.

    4) If your .Find.Execute specified a find style and that style was a character style, then you can be sure that that style would be reported, since a range found by character style can have only one character style assigned to it.

    5) much the same thinking should take place if, say the tail end of one paragraph in ParaStyle1 is selected with the head-end of the next paragraph formatted in, say ParaStyle2, except when I try that, I get a runtime error '91' "Object variable or With block variable not set",

  2. #2
    5 Star Lounger
    Join Date
    May 2001
    Stuttgart, Baden-W, Germany
    Thanked 0 Times in 0 Posts

    Re: What style is that? (Word97SR2)

    Hi Chris,

    I just posted the following code a few days ago, and came to the same conclusions.

    If you have more than 49 paragraphs selected, the style drop down will show nothing, so I expected the code to return "Nothing", too... didn' t happen, though (even with thousands of paragraphs, the paragraph style was returned).

    To excuse my code I can only say it *should* be easier to find out the styles. BTW, it's the first time I needed "Is Nothing" (to get around your point 5).

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

    <pre>Sub GetCharAndParaStyle()
    Dim myOldRange As Range
    Dim myStyle As Style
    Dim myCharStyle As String
    Dim myChar As Range
    Dim myParaStyle As String
    Dim strMsgPara As String
    Dim strMsgChar As String
    Set myOldRange = Selection.Range
    Set myStyle = Selection.Style
    strMsgPara = "Paragraph style: "
    strMsgChar = "Character style: "

    If myStyle Is Nothing Then
    strMsgPara = _
    "More than one paragraph style," & Chr(13) _
    & "or too many paragraphs selected." & Chr(13) _
    & "Styles at start of selection: " & Chr(13) _
    & "Para style: "
    Selection.Collapse (wdCollapseStart)
    Set myStyle = Selection.Style
    End If
    If myStyle.Type = wdStyleTypeParagraph Then
    myParaStyle = myStyle.NameLocal
    myCharStyle = ""
    myCharStyle = myStyle.NameLocal
    myParaStyle = Selection.Paragraphs(1).Style
    End If
    If myCharStyle = "" Then
    Selection.Collapse (wdCollapseStart)
    myCharStyle = Selection.Style
    If myCharStyle = "" Or myCharStyle = myParaStyle Then
    myCharStyle = _
    ActiveDocument.Styles(wdStyleDefaultParagraphFont) .NameLocal
    For Each myChar In myOldRange.Characters
    If myChar.Style <> myParaStyle Then
    strMsgChar = _
    "More than one character style in Selection. " _
    & Chr(13) & "First character style: "
    Exit For
    End If
    Next myChar
    strMsgChar = strMsgChar & _
    "More than one character style in selection; " _
    & Chr(13) & "First character style: "
    End If
    End If
    strMsgPara = strMsgPara & myParaStyle
    strMsgChar = strMsgChar & myCharStyle
    MsgBox strMsgPara & Chr(13) & strMsgChar, vbInformation
    End Sub</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