Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Mar 2002
    Location
    Sydney Australia
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Test for Style (Word 2000)

    I have an InputBox where the user nominates the paragraph style they want to use.

    How can I test to see whether a style of that name exists in a document? So that if the style does not exist the macro can respond:

    MsgBox "Style does not exist."

    Thanks in advance for your assistance.

    Apologies if this question has been answered before; I can't see where to access the Search function. Can someone remind me :-(

    PeteF

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Test for Style (Word 2000)

    Unfortunately, the Search facility has been disabled for quite a while now because it overburdened the server - see <post#=250092>post 250092</post#>.

    You could do something like this:

    Dim strStyle As String
    Dim stl As Style
    strStyle = InputBox("Enter a style")
    ' Get out if user canceled
    If strStyle = "" Then Exit Sub
    ' Be forgiving to errors
    On Error Resume Next
    ' Try to set a reference to the style
    Set stl = ActiveDocument.Styles(strStyle)
    ' Check if an error occurred
    If Not (Err = 0) Then
    MsgBox "Style doesn't exist"
    Exit Sub
    End If
    ' Reinstate normal error handling
    On Error GoTo 0 ' or to an error handler if possible

  3. #3
    New Lounger
    Join Date
    Mar 2002
    Location
    Sydney Australia
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Test for Style (Word 2000)

    Hans

    Thank you, that got me going.

    This worked:

    Private styleNumeric As Style

    Private sNumeric As String

    sNumeric = InputBox...

    ' Test for existence of style

    On Error GoTo NoStyle
    Set styleNumeric = ActiveDocument.Styles(sNumeric)

    NoStyle:

    MsgBox sNumeric + " Style does not exist in document."

    sNumeric = sNumeric + " (style does not exist)"

    I was surprised to find that the only (?) way to do this is to throw an error.

    Best regards

    PeteF

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Test for Style (Word 2000)

    Well, there is another way, but it's hardly efficient:

    Dim strStyle As String
    Dim stl As Style
    Dim fStyleExists As Boolean

    fStyleExists = False
    strStyle = InputBox("Enter a style")
    ' Get out if user canceled
    If strStyle = "" Then Exit Sub

    ' Loop through styles
    For Each stl In ActiveDocument.Styles
    ' Check name
    If UCase(stl.Name) = UCase(strStyle) Then
    ' Found!
    fStyleExists = True
    Exit For
    End If
    Next stl

    If fStyleExists = False Then
    MsgBox "Style doesn't exist"
    Exit Sub
    End If

Posting Permissions

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