sigil
sigil

Reputation: 9546

how to avoid "Compile Error: Can't find object or library"

I have a script that runs through a list of several thousand workbooks and picks up data from each one.

I'm getting the following error when the script tries to open certain workbooks:

Compile Error: Can't find project or library

I thought that I could avoid this message, which I understand is a due to problem with references, by opening workbooks without macros or alerts this way:

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

But this doesn't work; I still get the compile error. Is there any way to make sure this error doesn't pop up?

Upvotes: 2

Views: 910

Answers (1)

Peter Albert
Peter Albert

Reputation: 17495

You need to change the AutomationSecuritysettings:

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

Upvotes: 1

Related Questions