如何避免“编译错误:找不到对象或库”

时间:2013-01-16 01:22:43

标签: vba excel-vba excel

我有一个脚本,它运行在几千个工作簿的列表中,并从每个工作簿中获取数据。

当脚本尝试打开某些工作簿时,我收到以下错误:

Compile Error: Can't find project or library

我认为我可以避免此消息,我理解这是由于引用问题,通过打开没有宏的工作簿或以这种方式提醒:

dim bk as workbook
for each path in listOfWorkbookPaths
 Application.DisplayAlerts = False
 Application.EnableEvents=false
 set bk=workbooks.open(path)
 Application.EnableEvents=true
 Application.DisplayAlerts = True
 collectData(bk)
next

但这不起作用;我仍然得到编译错误。有没有办法确保没有弹出这个错误?

1 个答案:

答案 0 :(得分:1)

您需要更改AutomationSecurity设置:

Sub ImportFromExternalWorkbooks()
    Dim bk as Workbook
    Dim secAutomation As MsoAutomationSecurity

    secAutomation = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    For Each path in listOfWorkbookPaths
        Set bk=Workbooks.Open(path)
        collectData bk
    Next
    Application.AutomationSecurity = secAutomation 
End Sub