Solved

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

Posted on 2002-03-28
11
468 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
 
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
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.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

12 Experts available now in Live!

Get 1:1 Help Now