简体   繁体   English

VBA-我可以卸载堆栈调用以避免运行时28“堆栈空间不足”错误吗?

[英]VBA - Can I unload stack calls to avoid a runtime 28 “Out of stack space” error?

I'm working on a project that uses VBA UserForms to assist users in creating different Word documents. 我正在使用VBA UserForms帮助用户创建不同的Word文档的项目。 One of the requirements is that users are able to go back and forth between the Userforms. 要求之一是用户能够在用户表单之间来回切换。

This latest document has more UserForms (31 or so) than previous ones, and what I've found is that I can trigger a runtime error 28 "Out of stack space" by populating most of the forms, then navigating back to the first one, then going forward again. 最新的文档比以前的文档包含更多的UserForms(约31个),我发现可以通过填充大多数表单,然后导航回第一个表单来触发运行时错误28“堆栈空间不足” ,然后再次前进。

When going forward, I'm doing nothing different than the first time through, so I do believe that it truly is out of stack space due to a limit on the number of calls, rather than recursive or other problem listed in the Microsoft documentation here https://msdn.microsoft.com/en-us/library/aa264523(v=vs.60).aspx . 前进时,我的工作与第一次没有什么不同,因此,我确实认为由于调用数量的限制,它确实超出了堆栈空间,而不是此处Microsoft文档中列出的递归或其他问题。 https://msdn.microsoft.com/zh-CN/library/aa264523(v=vs.60).aspx

For the record, I am unloading the forms when going back or forward. 作为记录,我正在前进或后退时卸载表格。 Example: 例:

Private Sub cb_back_Click() 'backward navigation 

    Unload Me
    showPreviousForm 

End Sub

My question is, in VBA is there a way I can I "unload" a call from the stack? 我的问题是,在VBA中,有什么方法可以从堆栈中“卸载”呼叫?

It's extremely unlikely that a user would do what I did, so I'm not too concerned for this part of the project, but I would like to know in case other parts of the project are much closer to the limit. 用户极不可能执行我所做的事情,因此我对项目的这一部分不太担心,但是我想知道项目的其他部分是否接近极限。

Thanks in advance. 提前致谢。

The only way to "unload" a stack frame, is to return from that stack frame... or to blow everything up. “卸载”堆栈框架的唯一方法是从该堆栈框架返回...或炸毁所有东西。

I don't think your Unload Me does what you think it does, and even if it does work as intended, it's not going to help you with your call stack. 我不认为你Unload Me做什么你认为它,即使它确实像预期的那样,它不会帮助您与您的调用堆栈。

The problem is with calling showPreviousForm instead of returning to the caller: while the "previous form" is being displayed and until it's closed and execution runs to End Sub , then your call stack keeps track of that position inside cb_back_Click() , and until the call stack unwinds, it'll stay right where it is. 问题在于调用showPreviousForm而不是返回到调用者:在显示“上一个表单”时,直到它关闭并执行到End Sub为止,然后您的调用堆栈才在cb_back_Click()跟踪该位置,直到调用堆栈展开,它将保持原样。

You can observe this behavior using the Call Stack debug toolwindow. 您可以使用“ 调用堆栈”调试工具窗口来观察此行为。

There is no programmatic way to access the call stack in VBA. 没有编程的方法来访问VBA中的调用堆栈。 Anyone that ever tried to report a stack trace for error reporting wishes it was possible. 任何试图报告堆栈跟踪以进行错误报告的人都希望这是可能的。

The solution is to NOT Unload Me and store some state on your form instance that tells the caller where you need to go next; 解决方案是不要将Unload Me并在表单实例上存储一些状态,该状态会告诉调用方下一步您需要去哪里。 when End Sub is hit, the caller can then look at that state value and determine what to do. 当按下End Sub时,调用者可以查看该状态值并确定要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM