【问题标题】:Word VBA, end sub failsWord VBA,结束子失败
【发布时间】:2014-01-04 02:21:11
【问题描述】:

我有一个词vba,它使用excel,并且有几个程序调用:

 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

问题:从 testsub3 调用 testsub2 后,我无法在 testsub3 中结束 sub。在 MsgBox 之后,它跳转到 testsub3 中的一些代码 (documents("doc1").close)---error: bad file name ------>document has already been closed。

但如果它没有调用 testsub2,我可以结束。

想法?

谢谢

注意:我不使用循环,因为代码太长(错误:过程太大)。因此,多个程序/子。

【问题讨论】:

  • 循环在这里不起作用。代码太长,无法放入一个循环 - 导致错误:过程太大。这就是我创建多个潜艇的原因。

标签: vba ms-word


【解决方案1】:

也许您可以尝试以下方法:

 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

所以它只执行一次 testsub3 的最后部分。

【讨论】:

  • 这似乎可能导致他目前遇到的同样问题,除非所有数据都是连续的。
  • 我假设某个时候他会退出循环,然后第一个运行的 testsub3(堆栈顶部)会关闭文档,然后下面的(堆栈中较低的)不会尝试再次关闭文档。
  • 是的,但他正在对文件做一些事情,因此关闭它会引发类似于cannot do something when .... is closed 的错误
  • brWHigino,我试过你的代码,但同样的错误。出于某种原因,即使 boolean analysisComplete = true...strange,它仍然会跳回documents("doc").close
  • engineersmnky,我可以成功关闭文档(“doc”),但问题出现在 MsgBox 之后。关闭 MsgBox 后,错误突出显示行文档(“doc”).close。
【解决方案2】:

试过了,没有错误

 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

【讨论】:

  • 如果 testsub2 调用 testsub3(如您最初的示例所示),我认为您不应该从 testsub3 中 Call testsub2 (a,b,c,...)。我希望您的解决方案会在a &lt; somevalue 时立即失败。发生这种情况时,您将运行 testsub2 的第二个实例,这将启动 testsub3 的新实例,它将再次找到 a &lt; somevalue 并启动 testsub2 的第三个实例,等等。我认为您应该改为:if a &lt; somevalue then end sub或创建一个新的 sub 来执行您想要通过 testsub2 的第二个实例完成的操作(无需再次调用 testsub3 ...)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 2012-09-06
  • 1970-01-01
  • 2021-11-14
相关资源
最近更新 更多