Solved

Need help closing multiple files using the Dir function in Excel VBA

Posted on 2002-03-28
11
473 Views
Last Modified: 2008-02-01
I have an excel spreadsheet that opens a code sheet and then if exists, multiple branch*.xls files.  I previously used the Application.Filesearch function, but this no longer works with Windows 2000.  I am now using the dir function.  I can get all the files open, but when I exit Excel, I need to close these branch*.xls files without asking me whether I want to save, they are read only anyway.  Below is the code I am using.  Any help would be appreciated.

Sub HandleLocalizations(sourcedir As String, opening As Boolean)

'  allow the opening and run the auto open code for mulitple branch.xls files
'  these files must all be in the same directory as the point schedule tool and
'  must be named in the form branch*.xls, open read only in case on network
'  added flag for opening or closing, when closing, will call auto_close of
'  branch*.xls file(s) and will close w/o prompting for save, even if changes made

Dim filenames() As String, filenum As Integer
Dim i As Integer, justfilename As String, longname As String
FileCounter = 0
sourcedirfile = sourcedir + "\branch*.xls"
searchfile = Dir(sourcedirfile)
Do While searchfile <> ""
    PathandFile = sourcedir + searchfile
    FileCounter = FileCounter + 1
    ReDim Preserve filenames(FileCounter)
    filenames(FileCounter) = PathandFile
   
    If opening = True Then
        Workbooks.Open FileName:=PathandFile, ReadOnly:=True
        Application.Run Macro:=searchfile + "!Auto_Open"
    Else
         For Each w In Application.Workbooks
         longname = sourcedir + w.name
         If member(longname, filenames) = True Then
         Application.Run Macro:=w.name + "!Auto_Close"
         Application.DisplayAlerts = False
         w.Close SaveChanges:=False
    End If
    Next
    End If
    searchfile = Dir()
Loop
End Sub

Function member(item, list) As Boolean
' returns true if item is member of collection list, false if not
    Dim found As Boolean
   
    found = False
    For Each i In list
            If i = item Then
                found = True
            Exit For
            End If
    Next
    member = found
End Function
0
Comment
Question by:Tammi
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6903247
what's the error?
0
 

Author Comment

by:Tammi
ID: 6903264
There is no error, it just asks to close the file and I want it to close without asking.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6903304
odd, i just have:
w.Close SaveChanges:=False
   
and it doesn't prompt me (w/o that line it does)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 44

Expert Comment

by:bruintje
ID: 6903333
Hi Tammi, does this prompt too ? > w.Close False
0
 

Author Comment

by:Tammi
ID: 6903431
I have w.Close SaveChange:=False and I still get the prompt.  I also used w.Close (False) and still got the prompt.
0
 

Author Comment

by:Tammi
ID: 6903433
Currently I have three different branch*.xls files that it finds, one doesn't have an Auto_Close function in it, and that one closes without prompting, the other two I get the prompt to save.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6903570
so it's the auto_close playing up?

you could add a line there

ActiveWorkbook.Saved = True

i don't know if it works if you use it on another place in your code before closing like

Application.DisplayAlerts = False
ActiveWorkbook.Saved = True
w.Close False

:O)Bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6903577
or open them with this line added

ActiveWorkBook.RunAutoMacros xlAutoDeactivate

HTH:O)Bruintje
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 7637946
Hi Tammi,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept bruintje's comment(s) as an answer.

Tammi, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Author Comment

by:Tammi
ID: 7656577
This was so long ago I forgot what I did to fix the problem, but I know I didn't use any of the answers listed.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7659695
Tammi,
You did not bother to read my post.  What part of:

    DO NOT accept this comment as an answer.

is unclear?  And why have you ruined by GPA by insulting me with a C?  

-- Dan
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

778 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