Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why am I getting this error messgage? (Word 2002)

    Hi,

    I have the following procedure in my code, which when triggered produces the following error message:

    'Object variable or with Block variable not set'

    My procedure:

    Sub TestProc()

    Dim objPara As Paragraph

    For Each objPara In ActiveDocument.Paragraphs
    Debug.Print objPara.Style
    Next objPara

    End Sub

    This is driving me up the wall. Unless I am missing something completely obvious, I am at a complete as to why it won't work.

    Thanks, Chris

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

    Re: Why am I getting this error messgage? (Word 2002)

    Your code works for me (Word 2000 SR1a on Windows 98 SE) but it would be more correct to use...
    Debug.Print objPara.Style.NameLocal
    Since a Style is an object, not a text string.

    StuartR

  3. #3
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why am I getting this error messgage? (Word 2002)

    Hmmm.

    This is really weird. I am still receiving the same error. [img]/forums/images/smilies/sad.gif[/img]

    Any ideas?

    Chris

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

    Re: Why am I getting this error messgage? (Word 2002)

    Which line is generating the error?
    What happens if you type
    ? ActiveDocument.Paragraphs(1).Style.Namelocal
    in the immediate window?

    StuartR

  5. #5
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why am I getting this error messgage? (Word 2002)

    It is breaking on Debug.Print objPara.Style.NameLocal

    The strange thing is, if I type ?ActiveDocument.Paragraphs(1).Style.Namelocal in the debug window, then I get 'Heading 1'. No problem.
    However, if I type ?ActiveDocument.Paragraphs(2).Style.Namelocal in the debug window, then I receive the error.

    Stranger still, if I type ?ActiveDocument.Paragraphs(2).Range.Text in the debug window, then it works just fine.

    I have attached the problem document.

    Any ideas?

    Chris
    Attached Files Attached Files

  6. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Why am I getting this error messgage? (Word 2002)

    There does seem to be some sort of corruption in the document. If you click on the first line of the second paragraph, the selection appears to include the paragraph mark of the first paragraph. I inserted a new paragraph between the two and then deleted the new paragraph. I repeated this for the third and second paragraphs and it seems to have cleared the problem.

    Andrew C

  7. #7
    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: Why am I getting this error messgage? (Word 2002)

    I can't replicate the error. I tried a variety of views, and with and without field codes showing, which may be creating the strange effect Andrew noticed)

    The next time you get the error, can you open the Locals window and confirm that the document targeted as the ActiveDocument is correct? I guess there should be at least one paragraph in any document, but... I don't have a lot of other ideas.

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Why am I getting this error messgage? (Word 2002)

    Jefferson, I get the problem that Chris describes with this document, using Word 2002.

    If I click in the right margin of the first lines of paragraphs 2 and 3, I notice that the paragraph mark of the the previous paragraph is also selected. Entering and deleting a new paragraph between these pargraphs cleared the error message in my case.

    Andrew

  9. #9
    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: Why am I getting this error messgage? (Word 2002)

    Oh, I didn't look at the version. I tested in Word 2000. My bad. (Or, lucky me, as the case may be.)

    In Word 2000, the fact that there was an embedded paragraph in the first paragraph (by virtue of a TOC field code) didn't seem to bother VBA. Not sure why it's having problems in XP. Or maybe there's a new feature being suppressed that is giving me different results.

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

    Re: Why am I getting this error messgage? (Word 2002)

    This document does not look right even using Word 2000. If I select Paragraph 2 by typing ActiveDocument.Paragraphs(2).Range.Select in the immediate window then I get the attached selection. This is definitely caused by the embedded TOC fields at the ends of paragraphs 1 and 2. (Type Alt-F9 to see the fields.)

    What are these TOC fields for? They appear to be empty. If you just delete them (and then put the missing paragraph marks back in and reset the correct heading levels) then you will probably not have any more problems.

    StuartR
    Attached Images Attached Images
    • File Type: gif x.gif (5.5 KB, 0 views)

  11. #11
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why am I getting this error messgage? (Word 2002)

    Thanks for the feedback chaps.

    Stuart, I followed your instructions and everything seems to be ok now. I'm a little concerned though that the tool I am developing will continue to break on these sort of documents. Is there any way to 'clean' the document beforehand?

    Chris

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

    Re: Why am I getting this error messgage? (Word 2002)

    > Is there any way to 'clean' the document beforehand?
    I can't think of anything obvious, but this was a very strange error caused by an improbably constructed document and I wouldn't expect to see many of those.

    StuartR

Posting Permissions

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