Results 1 to 9 of 9
  1. #1
    PeterF
    Guest

    Identifying para style

    Could someone help me with the syntax for identifying the style of the current paragraph.
    In Word Basic it was:
    StyleName$()

    In Word Basic macro code recompiled for Word 97/2000, this translated to:
    Word Basic.[Stylename$]()

    I want to know what it is in native VBA.

    Thanks in advance

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

    Re: Identifying para style

    The following line of code will display the name of the style for the first paragraph of the current selection, in a message box:

    MsgBox Selection.Paragraphs(1).Style

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Identifying para style

    If your insertion point is in the paragraph, either of these seems to work and I don't know which is better or when they might differ:

    strSyleName = Selection.Paragraphs(1).Style
    strSyleName = Selection.ParagraphFormat.Style

  4. #4
    PeterF
    Guest

    Re: Identifying para style

    Thank you Jefferson and Gary for getting me started. What worked was:

    'test for the style of the current para
    Select Case Selection.ParagraphFormat.Style()

    'if the current para is Normal style...
    Case "Normal"

    'do something...

    Peter Fagan

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

    Re: Identifying para style

    Thanks Peter - I've learned something from you in return: I didn't know you could declare a Select Case with something like "Style()" - that is very interesting.

    You could of course also use:

    If Selection.ParagraphFormat.Style = "Normal" Then
    'do something
    End If

    Gary

  6. #6
    PeterF
    Guest

    Re: Identifying para style

    Gary

    My old "friend" (sort of) Microsoft Word Developer's Kit told me long ago that "The Select Case control structure executes faster than a complex If...Then...Else condition." That statement applied to WordBasic. Whether it holds true for VBA, I do not know; and whether execution speed is in any way an issue with Pentium 800+ PCs is another matter...

    I like the Select Case structure - I find it is easy to work with and to keep track of where you are.

    Thanks for the If...Then suggestion. I will note that for future reference!

    As Style() works, you might like to try some others - Font() for example.

    Peter Fagan

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

    Re: Identifying para style

    Hi again,

    You're probably right that the speed difference doesn't matter much these days...

    I tend to use If Then when there are only a couple of conditions to evaluate, and use Select Case if there are more than just a couple.

    The statement about Select Case executing faster may be a bit misleading though - I recall reading here some time ago (don't want to attribute it since I'm not sure I'm recalling correctly!) that a Select Case statement always evaluates all of the Case conditions (even if it has found a match on the first condition) whereas the If ElseIf etc. will stop evaluating once it finds a condition to be True.

    It would follow from that, that an If ElseIf construct would usually work faster, if you have a reasonable idea of the likelihood of each condition occuring, and order them from most likely to least likely. And the Select Case construct would be faster when each case has an equal likelihood of being true.

    In the end, it probably doesn't matter! <g>

  8. #8
    PeterF
    Guest

    Re: Identifying para style

    Gary

    Select Case is handy when many possibilities can be covered by the Case Else. For example:

    Case Style "A"

    Do this

    Case Style "B"

    Do that

    Case Else

    ' all other styles
    Do something else

    The structure neatly separates out the exceptions.

    "I recall reading here some time ago (don't want to attribute it since I'm not sure I'm recalling correctly!) that a Select Case statement always evaluates all of the Case conditions (even if it has found a match on the first condition)..."

    I believe I have read that also, and remember wondering why then, would a Select Case execute faster? Perhaps someone out there can explain...

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

    Re: Identifying para style

    I think Select Case is handiest when you have non-contiguous values. I posted a SHOCKING <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=vb&Number=25070&page=0&view =collapsed&sb=5>example</A> this week in the VBA forum.

    My example uses Select Case to iterate through "1", "2", "3", "4" etc. Responses to the therad show much better ways.

    For non-contiguous values, Select case is quite readable:
    <pre>(pseudocode)
    Select Case strtext
    Case "1"
    ...
    Case "4"
    ...
    Case "Alpha"
    ...
    Case "3 Foxcote Crescent"

    and so on


    </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
  •