tra*_*att 2 excel vba excel-vba
我正在尝试使用标准方法从Excel外部访问RhinoScript模块(在我的案例中是Windows 10上的Excel 365).
Dim Rhino As Object
Dim RhinoScript As Object
Set Rhino = CreateObject("Rhino4.Application")
Set RhinoScript = Rhino.GetScriptObject()
Run Code Online (Sandbox Code Playgroud)
但是我在最后一行遇到了"Runtime 424 - Object Required"错误.
问题似乎是GetScriptObject没有等待CreateObject完全完成,因为当Rhino4启动时,Windows 10的UAC将提示我允许Updater在允许它启动GUI之前对系统进行更改.如果我调试这些语句并逐行进行,以便在GetScriptObject命中之前Rhino4有时间完全启动,那么一切正常.那么,有没有办法让GetScriptObject等到Rhino4完全启动?我已经尝试设置一个循环来检查Rhino的值(Nothing?),但它会立即失效.我对VB有点新,所以对可能是一个新手问题道歉.
谢谢,
马特
我对Rhino API一无所知,可能有更好的方法来做到这一点.
如果您知道的是,运行时错误是由你所描述的是什么原因造成,而且Rhino.GetScriptObject是最终会成功,你可以循环,直到它的工作原理:
Dim iterations As Integer 'max value: 32,767
Do
iterations = iterations + 1 'will *eventually* overflow if loop keeps failing
On Error Resume Next 'switch off error handling
Set RhinoScript = Rhino.GetScriptObject
On Error GoTo 0 'restore error handling (IMPORTANT!)
If Err.Number <> 0 And Err.Number <> 424 Then Err.Raise Err.Number ' rethrow
Err.Clear
DoEvents ' so that the host app remains responsive while the loop runs
While RhinoScript Is Nothing
Run Code Online (Sandbox Code Playgroud)
如果数字不是预期的424,请注意重新引发错误; 如果情况无望,这是为了(试图)防止无限循环.
Nathan对增加额外的无限循环保护作出了很好的评论 - 这里通过递增计数器直到其值溢出其类型(提高运行时错误6).If如果32,768次迭代太长,则还可以使用常量和检查.
可能不是最好的解决方案,但应该有效.理想情况下,您将探索Rhino4.Application API,看看它是否公开了可用于确定实例是否"准备好"使用的任何成员.IsReady或者State也许吧.