简体   繁体   English

Excel VBA编译错误

[英]Excel VBA Compile Error

We have an excel spread sheet which we use and it works for most machines but bombs out with 'Compile Error in Hidden Module - General' on others, and the reason appears to be due to missing References. 我们有一个优秀的电子表格,可用于大多数机器,但在其他机器上却显示“隐藏模块中的编译错误-常规”,但原因似乎是由于缺少参考。

We check that Macros is enabled but still doesn't help. 我们检查是否启用了宏,但仍然无济于事。

Since we protect this excel spread sheet with a password, we don't want to be giving this password out to our users to check the References, and wondered if anyone had any idea how I can add VBA code to check whether the References required for the excel spread sheet is there and if not then bring up a message box to advise the user. 由于我们使用密码保护了此excel电子表格,因此我们不想将此密码提供给我们的用户检查参考,并且想知道是否有人知道如何添加VBA代码以检查是否需要参考excel电子表格已经存在,如果没有,则弹出一个消息框来建议用户。

The References we use are as follows: 我们使用的参考如下:

  • Visual Basic For Applications Visual Basic应用程序
  • Microsoft Excel 11.0 Object Library Microsoft Excel 11.0对象库
  • Microsoft Forms 2.0 Object Library Microsoft Forms 2.0对象库
  • Microsoft Windows Common Controls 5.0 (SP2) Microsoft Windows公共控件5.0(SP2)

Alternatively, if anyone has any other suggestions on how to go about this problem, that would be great. 或者,如果有人对如何解决此问题有任何其他建议,那将很棒。

The only reference you have listed that could possibly be missing is the common controls. 您列出的唯一可能会丢失的参考是通用控件。 The rest are default in every version of Excel. 其余的在每个版本的Excel中都是默认设置。 The Forms one is only if you have a userform or explicitly set it, but that's not your problem. 仅当您有一个用户窗体或显式设置它时,窗体一个窗体才是问题所在。 Common Controls is your problem. 通用控件是您的问题。 It doesn't ship with Office anymore. 它不再随Office一起提供。 If you have Visual Studio or VB6 you probably have it. 如果您拥有Visual Studio或VB6,则可能拥有它。 Or an old version of Office like XP Developer Edition. 或Office的旧版本,例如XP Developer Edition。

Anyway, you can check for the existence of the OCX file in the System folder. 无论如何,您可以检查System文件夹中是否存在OCX文件。 I don't think it's required to be in that folder, but I've never seen it anywhere else. 我不认为该文件必须位于该文件夹中,但我从未在其他任何地方看到它。

It's been quite a while since I've seen a reference to 5.0, so I included how to find 6.0 in the code below. 自从我看到对5.0的引用以来已经有一段时间了,所以我在下面的代码中包括了如何查找6.0。 Check to make sure you know what version you're using. 检查以确保您知道所使用的版本。

In a standard module: 在标准模块中:

Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Public Function HasCommonControl() As Boolean

    Dim sFolder As String
    Dim lReturn As Long

    Const lSIZE As Long = 255
    Const sDLLNAME As String = "COMCTL32.OCX" 'For windows common control 5.0
    'Const sDLLNAME As String = "MSCOMCTL.OCX" 'For windows common control 6.0

    sFolder = Space(lSIZE)
    lReturn = GetSystemDirectory(sFolder, lSIZE)

    sFolder = Left$(sFolder, lReturn)

    HasCommonControl = Len(Dir(sFolder & Application.PathSeparator & sDLLNAME)) > 0

End Function

Having said all that, why are you using common controls? 说了这么多,为什么要使用通用控件? If it's for a treeview on a userform, then check out this all-vba treeview 如果用于用户窗体上的树视图,则签出此全vba树视图

http://www.jkp-ads.com/articles/treeview.asp http://www.jkp-ads.com/articles/treeview.asp

Since jkp wrote that, I haven't used common controls. 自jkp编写以来,我就没有使用过通用控件。 So few normal-people PCs have it installed that it's just a pain. 很少有人安装PC,这很痛苦。

Depending on a reference to a specific Excel or other component version is one possible problem. 取决于对特定Excel或其他组件版本的引用是一个可能的问题。 Switching to late binding would solve that problem, so long as you're careful not to use any commands/objects/methods/properties that are supported in one version and not another. 只要注意不要使用一个版本而不是另一个版本支持的任何命令/对象/方法/属性,切换到后期绑定就可以解决该问题。

Following up on RowanC's link (good choice), you can add a reference to Excel, for example and write your code like so: 跟随RowanC的链接(很好的选择),您可以添加对Excel的引用,例如,编写代码,如下所示:

Dim xlWks as Excel.Worksheet
'Dim xlWks as Object

Once everything's debugged, remove the Excel reference and change the declarations to: 一切调试完毕后,删除Excel参考并将声明更改为:

'Dim xlWks as Excel.Worksheet
Dim xlWks as Object

That gives you the benefit of intellisense while coding/debugging but removes the dependency on a specific version of Excel later. 这使您可以在编码/调试时获得智能感知的好处,但稍后可以消除对特定版本Excel的依赖。

Might be mistaken, but IIRC the Common Controls component is part of Visual Basic, not Office, so unless you distribute it and register it along with your app, it might not be present on some systems. 可能会误解,但是IIRC Common Controls组件是Visual Basic而不是Office的一部分,因此,除非您分发它并将其与您的应用程序一起注册,否则它可能在某些系统上不存在。

To try late binding, which doesn't need the references setup (although it may have a performance hit, and it will mean that autocomplete doesn't work in your code) instead of calling excel in the following way: 要尝试后期绑定,不需要进行引用设置(尽管它可能会影响性能,并且这将意味着自动完成功能在您的代码中不起作用),而不是通过以下方式调用excel:

Dim oExcel As Excel.Application
  Set oExcel = CreateObject("Excel.Application")

try calling it this way, and drop the reference to the excel object model. 尝试以这种方式调用它,并删除对excel对象模型的引用。

 Dim oExcel As Object
   Set oExcel = CreateObject("Excel.Application")

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

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