工作簿打开期间编译错误 [英] Compile error during workbook open

查看:52
本文介绍了工作簿打开期间编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本使用某种类型的工作簿,我们称它为T,它位于我的参考文献中定义的模块(DLL)中–都很好.

I have a workbook which uses a certain type, let's call it T, that's in a module (DLL) defined in my References -- all good.

我创建了一些在Workbook_Open()中调用的代码,这些代码将添加对DLL的引用(如果它不在引用列表中).这样一来,我便可以将工作簿交给某个人,而他们不必手动创建参考.

I created some code that's called in Workbook_Open() that will add a reference to the DLL if it's not already in the list of references. This is so I can give the workbook to someone and they won't have to deal with creating the reference by hand.

我的问题是,当我打开工作簿(双击)时,在执行Workbook_Open()(并且可以设置Refence)之前,我被扔到调试器中,该调试器指向并抱怨类型T,该类型在T中定义.尚未定义尚未引用的DLL.好吧,不是在开玩笑.

My problem is that when I open the workbook (double-click), before Workbook_Open() gets executed (and the Refence can be set) I get thrown into the debugger which points to and complains that type T, defined in the not-yet-referenced DLL, is not defined. Well no kidding it's not.

这似乎有点小鸡和鸡蛋.有人看过吗?您是如何解决的?

This seems a little chick and egg. Anyone seen this before? How did you fix it?

推荐答案

就像其他人说的那样,如果要直到Workbook_Open才将对DLL的引用添加进去,所以必须使用后期绑定来连接到DLL中的内容.

Like other people have said, if you are not going to add the reference to the DLL until Workbook_Open, you have to use late binding to connect to the stuff in the DLL.

但是,我认为您这样做的方式是错误的.不必在Workbook_Open中添加对DLL的引用,而应已对其进行引用并检查.IsBroken.然后,您可以使用代码来完成修复损坏的引用所需的所有操作.

However, I think you are going about this the wrong way. Instead of adding a reference to the DLL in Workbook_Open, have it already referenced and check .IsBroken instead. Then you can have code to do what ever is needed to fix the broken reference.

这保留了早期绑定的好处,并且如果对DLL的引用被破坏,您将不会遇到相同的编译错误.

This keeps the benefit of early binding and you won't get the same kind of compile errors if the reference to the DLL is broken.

注意:您仍然会遇到编译错误,但它们不会用于缺少的DLL.当引用不完整时,如果函数未以Strings.开头,则任何使用VBA Strings模块中的函数的操作都会触发编译错误.例如,以下代码将在Workbook_Open运行之前导致编译器错误如果您的参考文献有误.

NOTE: You can still get compile errors, but they won't be for the missing DLL. While you have a broken reference, any use of functions from the VBA Strings module will trigger a compile error if the function isn't prefaced with Strings. For example, the following code will cause a compiler error before Workbook_Open runs if you have any broken references.

Private Sub Workbook_Open()
   Dim r As Reference
   For Each r In ThisWorkbook.VBProject.References
      If r.IsBroken Then
         MsgBox "Found broken reference." & vbCrLf _
              & Mid(r.FullPath, InStrRev(r.FullPath, "\") + 1)
      End If
   Next r
End Sub

要修复,请在MidInStrRev前面加上Strings..

To fix, prefix Mid and InStrRev with Strings..

MsgBox "Found broken reference." & vbCrLf _
     & Strings.Mid(r.FullPath, Strings.InStrRev(r.FullPath, "\") + 1)

这篇关于工作簿打开期间编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆