1
votes

I have a word vba that uses excel and has several procedures that call:

 private sub testsub1
     'some process

      call testsub2 (a, b, c,...)

 end sub

 private sub testsub2 (byref a as long, byref b as long, byref c as long,...)
     'some process

      call testsub3 (a, b, c,...)

 end sub

 private sub testsub3 (byref a as long, byref b as long, byref c as long,...)
     'some process

     'calls testub2 if a is less than some value.
      if a < somevalue then
         call testsub2 (a, b, c,...)
      end if

      documents("doc1").close    'closes a document
      wb.close 'closes a workbook
      exc.quit  'closes excel

      set wb = nothing
      set exc = nothing

      msgbox "Analysis complete"

 end sub

Question: I cannot end sub in testsub3 after calling testsub2 from testsub3. After MsgBox, it jumps to some code in testsub3 (documents("doc1").close)---error: bad file name ------>document has already been closed.

But I am able to end if it did not call testsub2.

Ideas?

Thanks

Note: I don't use loop because the code is too long (error: procedure is too large). Hence the multiple procedures/sub.

2
Loop doesn't work here. Code is too long to put in one loop - causes error: procedure is too large. This is why I created multiple subs. - user2468695

2 Answers

0
votes

Maybe you could try something like:

 Private analysisComplete As Boolean

 Private Sub testsub1()
     'some process

      analysisComplete = False

      Call testsub2(a, b, c,...)

 End Sub

 Private Sub testsub2(ByRef a As Long, ByRef b As Long, ByRef c As Long,...)
     'some process

      Call testsub3(a, b, c)

 End Sub

 Private Sub testsub3(ByRef a As Long, ByRef b As Long, ByRef c As Long,...)
     'some process

     'calls testub2 if a is less than some value.
      If a < someValue Then
         Call testsub2(a, b, c)
      End If

      If Not analysisComplete Then

        Documents("doc1").Close    'closes a document
        wb.Close 'closes a workbook
        exc.Quit  'closes excel

        Set wb = Nothing
        Set exc = Nothing

        MsgBox "Analysis complete"

        analysisComplete = True

    End If

 End Sub

So it executes the final portion of testsub3 only once.

0
votes

Tried this and worked, no errors

 private sub testsub3 (byref a as long, byref b as long, byref c as long,...)
     'some process

     'calls testub2 if a is less than some value.
      if a < somevalue then
         call testsub2 (a, b, c,...)

      else

         documents("doc1").close    'closes a document
         wb.close 'closes a workbook
         exc.quit  'closes excel

         set wb = nothing
         set exc = nothing

         msgbox "Analysis complete"

     end if

 end sub