Close a file that is not open

Posted on 2007-08-06
Last Modified: 2010-04-30
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?
Question by:isnoend2001
    LVL 27

    Expert Comment

    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

    Author Comment

    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
    LVL 85

    Accepted Solution

    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"
    LVL 27

    Assisted Solution

    >>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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Or just:


    (see my remark above)

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now