Solved

Open all workbooks with name in a particular pattern

Posted on 2013-06-05
2
299 Views
Last Modified: 2013-06-06
I have an ACCESS 2003 process that, when the user hits the Open button, it opens ALL Excel workbooks that have a particular name pattern.  I can open one, but I need to loop through the folder and open everyone with this pattern.

Private Sub cmdTodaysWorkbook_Click()
'/SS - Opens all workbook created with today's date.
        Dim xlApp As Excel.Application
        Dim wbk As Workbook
        Dim strExistingFileName As String
        Dim fld As Object
        Dim strSource As String

strExistingFileName = "\\SF1\User1\shared\ATMVisalosses\ATMVisaLossesTST_" & Format(Now, "mm-dd-yyyy") & ""

Set xlApp = New Excel.Application
xlApp.Visible = True



    For Each fld In strSource.SubFolders
            Set wbk = xlApp.Workbooks.Open(strExistingFileName & "*.xlsx")
            xlApp.Workbooks.Open (strExistingFileName & "*.xlsx")
    Next

xlApp.WindowState = xlMaximized
Set xlApp = Nothing

End Sub
0
Comment
Question by:ssmith94015
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 39223640
Using late binding...



Private Sub cmdTodaysWorkbook_Click()
'/SS - Opens all workbook created with today's date.
    Dim xlApp As Object
    Dim strExistingFileName As String
    Dim fso As Object
    Dim fil As Object
    Dim fld As Object
    Dim sf As Object

    Const StartFolder As String = "\\SF1\User1\shared\ATMVisalosses\"

    strExistingFileName = "ATMVisaLossesTST_" & Format(Now, "mm-dd-yyyy")

    Set xlApp = CreateObject("Excel.Application"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(StartFolder)

    For Each fil In fld.Files
        If fil.Name Like strExistingFileName & "*.xlsx" Then
            xlApp.Workbooks.Open fil.Path
        End If
    Next

    For Each sf In fld.SubFolders
        For Each fil In sf.Files
            If fil.Name Like strExistingFileName & "*.xlsx" Then
                xlApp.Workbooks.Open fil.Path
            End If
        Next
    Next

    xlApp.Visible = True
    xlApp.WindowState = xlMaximized
    Set xlApp = Nothing
    Set fil = Nothing
    Set sf = Nothing
    Set fld = Nothing
    Set fso = Nothing

End Sub

Open in new window

0
 

Author Closing Comment

by:ssmith94015
ID: 39227489
Yes, it was getting around to the Like statement, I did not think it would work.

Sandra
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

840 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