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

    para.range.style problem (Word XP VBA)

    I am running to a problem here. I will attach this sample template, in which I have isolated the problem.

    As you can see, when you run the MAIN macro (represented on the toolbar), the code has issue with the first cell. If I delete that cell--or even the contents--the code works fine. But I'm not sure why. What makes the first cell so different from the second cell? Why does the first cell trigger ther error while the second one is fine?

    I'm afraid I'm ignorant in this matter. I hope for someone to identify the difference and reveal it to me. If you can do that, I'd really appreciate it!

    I imagine there is something interesting in the text, but I can't pinpoint it. I'd like to know what I'm looking for so I can bypass it in my code.

    Thanks
    Kevin Elmore

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: para.range.style problem (Word XP VBA)

    What was the error? I ran the code, and didn't get any errors.

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

    Re: para.range.style problem (Word XP VBA)

    My impression is that there is some kind of corruption in the paragraph ends in the first cell. If you delete them, then type them in again, the error is gone. You can also replace all ^p (paragraph end) by something like @@@ and back.

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

    Re: para.range.style problem (Word XP VBA)

    What was the error? I ran the code, and didn't get any errors.

    What version of Word are you using? I get the error in XP. I suppose this could be a nonissue in 2003.

    I receive Run-Time Error 91: Object Variable or With block variable not set.
    When I debug, the offending statement is: Select Case para.Range.Style

    I gather that the system is having a hard time accessing the style of that particular paragraph. When I step through this code, it selects the two paragraphs in the first cell just fine and runs the code. But the third "paragraph" selects the entire first cell. This is what causes the problem. It's not a problem with the second cell, even when the entire cell is checked.

    As suggested, perhaps it is simply a corrupt document (in which case, Word 2003 may be able to handle this corruption). In that case, there probably isn't much I can do with the code. I've run this code many times before. This document is part of a much larger document which undoubtedly has gone through a lot of style changes, table manipulations, and other heavy-duty work that can bog it down.

    I'm going to assume that this is simply a corrupt document and that the code is sufficient for my needs. I'll check back on this thread (and have my notify on) for any other suggestions.

    Thanks for looking into this.

    Kevin

  5. #5
    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: para.range.style problem (Word XP VBA)

    I agree with the corruption hypothesis (testing in Word 2002). If you prefer not to select the entire cell, perhaps you simply should comment out the .Select line? I don't think you really need it after debugging.

  6. #6
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: para.range.style problem (Word XP VBA)

    Actually, it's not corruption at all (well, OK, there was apparently corruption with the List Styles in the copy I downloaded, but it had nothing to do with the error.) It is a problem with how VBA handles (or doesn't handle) the scenario where there is more than one style to be found when an entire cell is selected (which always happens when trying to select the last paragraph in a cell.) And sorry about the run-on sentence.

    Two things bear out this conclusion: First, if all of the paragraphs in a cell have the same style, the error isn't triggered. (Try changing them all to the same style and see.) Second, the error isn't triggered if the range with more than one style is not in a cell. (I set up two paragraphs of different styles before the table and stepped through the code, stopping to expand the range manually, and the error was not triggered.)

    It's also interesting that this problem occurs even if you aren't working with a selection object (which I always try to avoid anyway, because working with Ranges tends to be faster and allows you to leave the selection exactly where it was when the user chose the command.)

    A method that worked for me was to dimension a range variable, set it equal to the current paragraph, and then collapse it to the beginning of the paragraph before evaluating its style; this prevented the error from occurring because it doesn't end up "selecting" the whole cell.

    <font face="Georgia">Public Sub MAIN()

    Dim para As Paragraph
    Dim docCurrent As Document, curRange As Range

    Set docCurrent = ActiveDocument

    For Each para In docCurrent.Paragraphs

    ' Select the paragraph so the code can be followed.
    Set curRange = para.Range
    curRange.Collapse wdCollapseStart
    curRange.Select

    Select Case curRange.Style

    Case docCurrent.Styles(wdStyleHeading1), _
    docCurrent.Styles(wdStyleHeading2), _
    docCurrent.Styles(wdStyleHeading3), _
    docCurrent.Styles(wdStyleHeading4), _
    docCurrent.Styles(wdStyleHeading5), _
    docCurrent.Styles(wdStyleHeading6), _
    docCurrent.Styles(wdStyleHeading7), _
    docCurrent.Styles(wdStyleHeading8), _
    docCurrent.Styles(wdStyleHeading9)

    ' Do things here....

    End Select

    Next para

    MsgBox "Test has been completed. The error is gone."

    End Sub
    </font face=georgia>
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

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

    Re: para.range.style problem (Word XP VBA)

    Good detective work! Thanks for sharing it.

  8. #8
    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: para.range.style problem (Word XP VBA)

    If I create a new document, new table, and style the paragraphs differently, this runs with only one error:

    <pre>Sub DumpStyles()
    Dim par As Paragraph
    For Each par In ActiveDocument.Paragraphs
    Debug.Print "par.Style = " & par.Style
    Debug.Print "par.Range.Style = " & par.Range.Style
    Debug.Print "par.Range.ParagraphFormat.Style = " & _
    par.Range.ParagraphFormat.Style
    Debug.Print "---"
    Next
    End Sub</pre>

    I wonder why the first two methods of getting the Style work and the third is unhappy? After all, the range, par, hasn't changed as a result of doing a select. And I deleted the Select anyway.

    When I say "work," I must qualify that, since they give different results:

    par.Style = Normal
    par.Range.Style = Table Grid
    par.Range.ParagraphFormat.Style = Normal
    ---
    par.Style = Body Text
    par.Range.Style = Body Text
    par.Range.ParagraphFormat.Style = Body Text
    ---
    par.Style = Plain Text
    par.Range.Style = Table Grid
    Error occurs at this point

    Perhaps a way to address this is to check this for truth:
    <code>par.Range.End = par.Range.Cells(1).Range.End</code>
    How to avoid the error, I'm still not sure; collapsing the range didn't seem to do anything for me. <sigh>

Posting Permissions

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