none
"Not responding" problems with userform despite DoEvents RRS feed

  • Question

  • Hi.

    I have a userform that displays when running code in Word and it's all working fine with most documents.

    However, I am finding if adding a table of contents in a very large document, I get (in Word 2007) a 'Not responding' userform. It happens on the line:

        With oDoc
            .TablesOfContents.Add Range:=Selection.Range, _
            UseHeadingStyles:=True, UpperHeadingLevel:=1, _
            LowerHeadingLevel:=1, IncludePageNumbers:=True
        End With

    Despite DoEvents before and after, adding a large table of contents (e.g. with 100 items) causes a 'Not responding' userform and sometimes the userform will then disappear. You can get it back by clicking in the document when it encounters the next DoEvents but it looks messy to have a 'Not responding' and disappearing userform, even if for a brief period.

    With, say, 30 items in the table of contents it's fine, but try it with 90 or 100 items and it takes that bit longer for Word to add and it's long enough to trigger a 'Not responding' state.

    Any ideas on how to fix? Have had an Internet search, but can't find anything. I am wondering if there is no way to solve this.

    Thursday, July 12, 2012 8:17 PM

Answers

  • In case it helps anyone, I was able to solve the disappearing userform by creating a blank userform and using a Kernel32.dll sleep function to display this for a few brief milliseconds before unloading. You can show this off screen by setting the top to minus.

    Showing this after the 'not responding' event will stop the main userform disappearing. 

     
    • Marked as answer by JFK555 Sunday, February 3, 2013 10:17 AM
    Sunday, February 3, 2013 10:16 AM

All replies

  • By the way, I found that Word 97 (pretty old but still very fast!) doesn't have this problem. It looks like it is Word 2007's repagination when adding a large table of contents to a large document that is causing the delay and thus the 'Not responding' state. Repagination is unavoidable adding a table of contents it seems (and code runs whilst doc is in Draft view) but problem to me seems not so much Repagination as fact that a line of code that takes some time will trigger 'Not responding' and DoEvents is no use in that scenario.

    I guess I should really port to .NET but would prefer to find out first if there is any quick fix for this!

    Friday, July 13, 2012 7:43 AM
  • you should add DoEvents commend inside loop like:

    sub abc_click() 
    for each el in conteiner 'Or x = 1 to contener.count 
    DoEvents
    on error goto serr 
    'something with el next
    
    exit sub
    serr:msgbox err.description 
    end sub 
    
    'Where conteiner is collection

    You can add to form Progressbar -How does it work


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Monday, July 16, 2012 9:06 AM
    Answerer
  • Thanks, but the line is not in a loop. It's simply a single action that takes a long time if the document is very large. As I say, DoEvents of no use here, as far as I can tell, even if you put it directly before and after the action.

    By the way, I tried it on the new Word 2013 preview. Once it ran fine, no 'Not responding' problem. Second time, it showed 'Not responding' as Word 2013 paginates for a (relatively) long time as a table of contents with 96 items is added. Then the Userform disappeared...

    Tuesday, July 17, 2012 7:12 AM
  • In case it helps anyone, I was able to solve the disappearing userform by creating a blank userform and using a Kernel32.dll sleep function to display this for a few brief milliseconds before unloading. You can show this off screen by setting the top to minus.

    Showing this after the 'not responding' event will stop the main userform disappearing. 

     
    • Marked as answer by JFK555 Sunday, February 3, 2013 10:17 AM
    Sunday, February 3, 2013 10:16 AM