简体   繁体   中英

Excel VBA using Workbook.Open with results of Dir(Directory)

This seems so simple and I've had it working multiple times, but something keeps breaking between my Dir call (to iterate through a directory) and opening the current file. Here's the pertinent code:

SourceLoc = "C:\ExcelWIP\TestSource\"
SourceCurrentFile = Dir(SourceLoc)

'Start looping through directory
While (SourceCurrentFile <> "")
Application.Workbooks.Open (SourceCurrentFile)

What I get with this is a file access error as the Application.Workbooks.Open is trying to open "C:\\ExcelWIP\\TestSource\\\\FILENAME" (note extra slash)

However when I take the final slash out of SourceLoc, the results of Dir(SourceLoc) are "" (it doesn't search the directory).

The frustrating thing is that as I've edited the sub in other ways, the functionality of this code has come and gone. I've had it work as-is, and I've had taking the '/' out of the directory path make it work, and at the moment, I just can't get these to work right together.

I've scoured online help and ms articles but nothing seems to point to a reason why this would keep going up and down (without being edited except for when it stops working) and why the format of the directory path will sometimes work with the final '/' and sometimes without.

any ideas?

This would open all .xlxs files in that directory son.

    Sub OpenFiles()
    Dim SourceCurrentFile As String
    Dim FileExtension as String: FileExtension = "*.xlxs"
    SourceLoc = "C:\ExcelWIP\TestSource\"
    SourceCurrentFile = Dir(SourceLoc)
    SourceCurrentFile = Dir()
    'Start looping through directory
    Do While (SourceCurrentFile <> "")
    Application.Workbooks.Open (SourceLoc &"\"& SourceCurrentFile)
    SourceCurrentFile = Dir(FileExtension)
    Loop
    End Sub

JLILI Aman hit on the answer which was to take the results of Dir() as a string. Using that combined with the path on Application.Open allows for stable behaviors from the code.

New Code:

Dim SourceLoc as String
Dim SourceCurrentFile as String
SourceLoc = "C:\ExcelWIP\TestSource\"
SourceCurrentFile = Dir(SourceLoc)

'Start looping through directory
While (SourceCurrentFile <> "")
Application.Workbooks.Open (SourceLoc & "/" & SourceCurrentFile)

I didn't include the recommended file extension because I'm dealing with xls, xlsx, and xlsm files all in one directory. This code opens all of them.

Warning - this code will set current file to each file in the directory including non-excel files. In my case, I'm only dealing with excel files so that's not a problem.

As to why this happens, it does not appear that Application.Open will accept the full object results of Dir(), so the return of Dir() needs to be a String. I didn't dig deeper into the why of it beyond that.

Consider using VBA's FileSystemObject which includes the folder and file property:

Sub xlFilesOpen()
    Dim strPath As String
    Dim objFSO As Object, objFolder As Object, xlFile As Object

    strPath = "C:\ExcelWIP\TestSource"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strPath)

    For Each xlFile In objFolder.Files
        If Right(xlFile, 4) = "xlsx" Or Right(xlFile, 3) = "xls" Then
            Application.Workbooks.Open (xlFile)
        End If
    Next xlFile

    Set objFSO = Nothing
    Set objFolder = Nothing
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM