Error Handeler for Workbooks.Open delay?

USAMax

Well-known Member
Joined
May 31, 2006
Messages
846
Office Version
  1. 365
Platform
  1. Windows
I have a loop that goes through every xlxs & xlsm file in a SharePoint folder but every now and then it hangs while opening a random workbook. It can run fine most of the time but other times it hangs.

This is the command I use to open the file:
Workbooks.Open Filename:=strSharePointPath _
& strWorkbookName, _
ReadOnly:=True ' Open the Current Workbook as Read Only

Is there a way to detect when it hangs too long? If so, I need it to GoTo ReportWorkbookNotProcessed.

Thank you...
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You might be able to use Application.OnTime before the Workbooks.Open line and if the workbook is successfully opened, cancel that Application.OnTime statement.
I am not sure if the OnTime procedure will execute if the program is waiting for a file to be opened. See VBA help on this function for more info.

Code:
Option Explicit

Sub ScheduleAProcess()


    Dim dteTime As Date
    Dim strSharePointPath As String
    Dim strWorkbookName As String
    
    'Schedule the procedure to run 1 min from now
    dteTime = Now + TimeValue("00:01:00")
    Application.OnTime dteTime, "ReportWorkbookNotProcessed"   'This must be a separate sub/function, not part of the same one.
    Application.StatusBar = "RWNP Subroutine Scheduled"
    
    On Error Resume Next
    Workbooks.Open Filename:=strSharePointPath _
        & strWorkbookName, _
        ReadOnly:=True ' Open the Current Workbook as Read Only
    If Err.Number = 0 Then
        'File Opened with no problem
        Application.StatusBar = False
        'Cancel the pending procedure
        Application.OnTime EarliestTime:=dteTime, _
            Procedure:="ReportWorkbookNotProcessed", Schedule:=False
    End If
    On Error GoTo 0
    Application.StatusBar = False
    
End Sub

Check out the answers to this post as a possible way to see why they don't open:
https://answers.microsoft.com/en-us...-file-is/381e9f29-50a5-4fa3-b9f2-7b3389293063
 
Upvote 0
Phil,

I cannot thank you enough for responding. If there was an Error that I could test for then I could add an, "On Error GoTo..." but for some reason it just hangs and I have to end task. I never used the Application.OnTime before so I had to try it.

I am using the Dir() function so the files do exist but network issues and/or open files are producing long delays in opening the files.

I am very open to any suggestions as user complaints are plaguing me!
 
Upvote 0
I stand corrected, the issue is during the Dir() command where it is getting the name of the next file to open.

I created a userform to display every step so I could see where it is hanging.

I believe the question should still be the same.
 
Upvote 0
One possible solution would be use Dir to get all of the filenames into an array then open them using the names stored in the array.
Stick a couple of DoEvents in the Dir loop so you will be able to use Ctrl+Break to stop then restart the loop.
 
Upvote 0
Phil,

It turns out the issue was when it tried to open the file and not the Dir. After that day the issue went away for me and the users!

Just wanted to let you know and to say thank you! Your help was very much appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top