getfile() randomly failing

Status
Not open for further replies.

roscoe

Well-known Member
Joined
Jun 4, 2002
Messages
1,046
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have code that imports the contents of a text file. On rare occasions it fails. Here's the code in question (please ignore typos as they are not relevant to the problem but merely reflect my poor typing skills):

Set urlObj = CreateObject("Scripting.FileSystemObject")
Set cHTML = urlobj.getfile(TextFileName)
Set cinfo = cHTML.OpenAstextStream(1,0)

Note: I stole this code from someone else who was reading in html files and until now has worked fine for non-HTML text files (if that mattered at all). In fact I use it in multiple locations (i.e. opening other text files) and it has NEVER failed (assuming the file actually exists).

The issue: On rare occasions (randomly and unable to reproduce "on command") the code just stops at the second line above relative to a specific file. I've found that if I select "debug" to open the code I can then just hit F5 (without doing anything else) and it will continue normally. More often than not it doesn't fail and continues through this step normally.

What I have observed is that the the specific file being opened (i.e the one that is failing) is very large (>750,000 lines). I suspect that because of the size it takes a smidge longer than normal for the operating system (OS) to process the line and therefore is subject to failing. I'm wondering (seeking your wise guidance here) if there's some code I can add that will maybe add a few "beats" to the code around this line to allow the OS to keep up and prevent the random fails.

Thanks in advance!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Sorry for the double tap...first submission appeared to have failed so I resubmitted. Admin can delete this post if desired
 
Upvote 0
"DoEvents" does what you've described. Just add as a standalone line of VBA where you want the OS to keep up - immediately after your second line. Good luck
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,826
Messages
6,121,793
Members
449,048
Latest member
greyangel23

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