Waiting for file to create before reading

adbrad

New Member
Joined
Jan 5, 2018
Messages
3
Office Version
  1. 365
Platform
  1. Windows
For a single macro, I want to create a file in an outside program, wait for it to create, open it to read it. I can use Wait or Sleep while checking if the file exists. I was looking for code to check for the file, but wait until it's completely created to open it. Would trying to open it while it's still creating show an error or does FileExists returns true if the file is still being created?

I tried googling and looking through this forum, but can't find correct answer or can't figure out key words in the search.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Please try this:

Run the LoopFileExist to start. Edit the file name in the NextCheckFileExist SUB as needed

VBA Code:
Public BegTime As Date
Public CheckCount As Long

Public Function FileExist(PFN As String) As Boolean
  Dim TempFN As String
  TempFN = Dir(PFN, vbNormal)
  If TempFN <> "" Then FileExist = True
  
End Function


Sub LoopFileExist()

  BegTime = Now() + TimeValue("00:00:5")
  CheckCount = 0
  Application.OnTime BegTime, "NextCheckFileExist", Schedule:=True   'Schedule NextTime
  
End Sub


Sub NextCheckFileExist()
  If FileExist("C:\Temp\Jeff.txt") Then
    MsgBox "Your file is ready to read"
  Else
    CheckCount = CheckCount + 1
    Debug.Print CheckCount
    If CheckCount > 10 Then Exit Sub
    BegTime = Now() + TimeValue("00:00:5")
    Application.OnTime BegTime, "NextCheckFileExist", Schedule:=True   'Schedule NextTime
  End If
  
End Sub
 
Upvote 0
Thanks for your quick reply. Would this work if the file was being written to (0 bytes) at the time of FileExists check? Or could you use IsFileOpen to check that?
 
Upvote 0
This will tell that a file exists. Here is some code I have to determine if the file is open by another App or person. Some text editors don't lock the file so this returns false.

VBA Code:
Function FileLocked(strFilename As String) As Boolean
   On Error Resume Next
   ' If the file is already opened by another process,
   ' and the specified type of access is not allowed,
   ' the Open operation fails and an error occurs.
   Open strFilename For Binary Access Read Write Lock Read Write As #1
   Close #1
   ' If an error occurs, the document is currently open.
   If Err.Number <> 0 Then
      ' Display the error number and description.
      MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
      FileLocked = True
      Err.Clear
   End If
End Function
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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