locked
random run-time error 5941 when selecting a cell in a table RRS feed

  • Question

  • Hi All,

    I have a problem with a template which randomly generates the run-time error '5941 the requested member of the collection does not exist'.  What seemed strange is that it works fine sometimes and others it would generate the error.  After much searching, I have discovered that the code is not executing sequentially (ie. it is essentially executing in a multi-threaded or asynchronous fashion).  The code structure looks a like this:

    Private Sub AutoNew

        Some code A

             Load Form1
                  Code for Form1

        Some Code B

            Load Form2
                Code for Form2

       Some Code C

    End Sub

    Basically, a few simple lines of code execute, then a form is loaded.  The user completes the form and then clicks OK.  The code in the form then moves around the document inserting text.  At the completion of the form code, the cursor is in the second cell of the last row of a table.  This works EVERY time.

    In the Code B part of the AutoNew procedure, the code should select the content of the cell.  This works most of the time, but when it fails and generates the error, the Form1 code hasn't finished executing yet.

    How do I stop Code B from executing before Form1 Code is finished executing.

    Cheers


    Stephen, RedGalaxy Darwin, Australia

    • Moved by Cindy Meister MVP Monday, February 18, 2013 11:14 AM VBA & Word-specific, not using VSTO
    Monday, February 18, 2013 6:52 AM

Answers

  • The solution has to re-write the code so that there aren't possibilities for the code in the main module to execute while a form is being displayed.  Essentially what I have done is taken Code Block B and incorporated it into Form 1.  Also, Form 2 was being used as a notification to the user.  I have substituted that with a msgbox the is part of Form 1 cmdOK_Click procedure.

    So, while Word was misbahaving by executing subsequent code before previous code was finished executing, it turns out my predecessor had poorly written the solution and a rewrite was required.


    Stephen, RedGalaxy Darwin, Australia

    • Marked as answer by RedGalaxy Tuesday, February 26, 2013 1:23 AM
    Tuesday, February 26, 2013 1:23 AM

All replies

  • Hi Stephen

    Please show us the actual code that declares and "loads" the two forms.

    Also, I assume the user clicks a button ("OK") and code executes that eventually places the cursor in the specific cell. Please show us what happens to the form at that point. (Are you using Hide, Unload...?)

    Finally, what version of Word is involved?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 18, 2013 11:25 AM
  • Hi Cindy,

    I can't post the code because it isn't mine. I have been asked to resolve an issue with moving the templates from 2003 to 2010. In 2003 the code executes perfectly every time but in 2010 Code block B is executing before Form 1 code is finished executing.  And the form is being unloaded.

    I have in fact resolved the issue by rewriting the code.  However, the fact remains that VBA should execute one line at a time and in Word 2010 (as with Access 2010) it isn't.  I just need to know how to force it to execute all the code in the form before it continues with the module.  In Access, I can create the form instance as a dialog box thus forcing all other code to halt until the form is complete.  I don't see a way to do that in Word.

    Cheers


    Stephen, RedGalaxy Darwin, Australia

    Tuesday, February 19, 2013 4:48 AM
  • If you can't give us sample code / steps to reproduce the problem, it's not possible to help you...

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, February 20, 2013 7:26 AM
  • The solution has to re-write the code so that there aren't possibilities for the code in the main module to execute while a form is being displayed.  Essentially what I have done is taken Code Block B and incorporated it into Form 1.  Also, Form 2 was being used as a notification to the user.  I have substituted that with a msgbox the is part of Form 1 cmdOK_Click procedure.

    So, while Word was misbahaving by executing subsequent code before previous code was finished executing, it turns out my predecessor had poorly written the solution and a rewrite was required.


    Stephen, RedGalaxy Darwin, Australia

    • Marked as answer by RedGalaxy Tuesday, February 26, 2013 1:23 AM
    Tuesday, February 26, 2013 1:23 AM