Solved

Open all workbooks with name in a particular pattern

Posted on 2013-06-05
2
295 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
Comment Utility
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
Comment Utility
Yes, it was getting around to the Like statement, I did not think it would work.

Sandra
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

771 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

10 Experts available now in Live!

Get 1:1 Help Now