Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Apr 2003
    Location
    Kansas City, Missouri
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What causes a style to be Nothing? (Word 97/SR-2 and 2000)

    Hello. I am programmatically checking a document's styles paragraph by paragraph and returning a report if Heading styles are applied to table cells or page breaks.

    For the most part, the code works fine. The beginning of the loop is:

    For Each para In docCurrent.Paragraphs
    Select Case para.Range.Style
    Case "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Heading 5", _
    "Heading 6", "Heading 7", "Heading 8", "Heading 9", "Table Title", _
    "Figure Title"

    Usually, this works fine. I have been shown a sample document where it doesn't work. After some error trapping and whittling things down, I discovered that a paragraph, which has a style of Normal shows up as Nothing in VBA. I hover over the para.Range.Style and see that the style is Nothing. This causes my code to bomb.

    I tried to bypass this by having
    If para.Range.Style <> Nothing
    but apparently, VBA doesn't like Nothing being used that way.

    From a VBA POV, how can I trap a "Nothing" paragraph?

    From a Word POV, what causes a paragraph to have no style? I'm sure this is a problem only for VBA, but I want to see if anyone else has this sort of problem. Furthermore, the problem happens when I have a document attached to the template. If I run the code on the template itself, the error does not show.

    I appreciate any insight into this. I am cross-posting this to Word.

    Kevin

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: What causes a style to be Nothing? (Word 97/SR-2 and 2000)

    You could try If ISNULL(Para.Range.Style) Then ... (I have no idea if this will work, I haven't seen the issue you describe)

    A style is an object, its default property is .NameLocal, which is "Heading1" etc. Another change I would make to your code is to compare Style.NameLocal to Application.Styles(wdStyleHeading1) etc. This will enable your code to work in other languages and avoids the assumption that the NameLocal property is the default. It is much better to be explicit.

    StuartR

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

    Re: What causes a style to be Nothing? (Word 97/SR-2 and 2000)

    Try If Not (para.Range.Style Is Nothing) Then. Keywords such as Null and Nothing are not values in the normal sense; you must use the special keyword Is in comparisons involving them.

    Perhaps you can post a sample document - you can remove most of its contents, as long as the offending paragraph remains. Don't forget to check File | Properties to remove sensitive information.

  4. #4
    Star Lounger
    Join Date
    Apr 2003
    Location
    Kansas City, Missouri
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What causes a style to be Nothing? (Word 97/SR-2 and 2000)

    Thank you, Hans. I forgot that I must use "Is Nothing." That did the trick.

    I tried IsNull, as well. I forgot to include that in my original post. But "Is Nothing" worked beautifully. I forget about that.

    Thanks.
    Kevin

Posting Permissions

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