Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Close a file that is not open

Hi experts
I am downloading file from the internet with freefile
FileOne = Freefile
FileTwo = Freefile
FileThree = FreeFile
After each file is downloaded then
Close #FileOne, Close #FileTwo, Close #FileThree
The connection could be interupted at any point or the form closed
Is this wrong?
on error resume next
Close #fileOne
close #filetwo
close #file Three
On error goto error handler
I realize i could write a module boolean for each file and set it to true when they get succeffuly opened and then downloaded and then selectly close them close them.
Sure be a lot easier to just close them all even if they are not open.
Bad idea?
  • 2
  • 2
  • 2
2 Solutions
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
       "URLDownloadToFileA" (ByVal pCaller As Long, _
       ByVal szURL As String, _
       ByVal szFileName As String, _
       ByVal dwReserved As Long, _
       ByVal lpfnCB As Long) As Long

Public Sub SaveURLToFile(ByVal sUrl As String, ByVal sFileName As String)
   Call URLDownloadToFile(0&, sUrl, sFileName, 0, 0)
End Sub

Above code will download,open,save and close files automatically and asyncroneously
isnoend2001Author Commented:
I am not looking for a way to download the files. I want to intercept any error when downloading either by connection unplugged or user cancel during download. if the file is closed they can restart the download.
Just want to know if closing files that are not open is a problem
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just use the "Close" statement without any file numbers.  Any open files will be closed automatically...

From the docs:

    "If you omit filenumberlist, all active files opened by the Open statement are closed"
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

>>Just want to know if closing files that are not open is a problem<<
No problem:
Private Sub Command1_Click()
   For i = 1 To 255
      Close i
   Next i
End Sub
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or just:


(see my remark above)
isnoend2001Author Commented:
Thanks guys
Thats good to know "Close" by itself will close all the open files. In this case i will have to close just the 3 files on this form. Other forms may have files open that i don't want closed. Just wanted to be sure closing a file by name that was not open would not get me later.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now