简体   繁体   English

使用Workbook的Excel VBA。打开Dir(目录)的结果

[英]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. 这看起来很简单,我已经多次工作了,但是我的Dir调用(迭代目录)和打开当前文件之间的某些东西一直在断开。 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) 我得到的是文件访问错误,因为Application.Workbooks.Open正在尝试打开“C:\\ ExcelWIP \\ TestSource \\\\ FILENAME”(注意额外的斜杠)

However when I take the final slash out of SourceLoc, the results of Dir(SourceLoc) are "" (it doesn't search the directory). 但是当我从SourceLoc中取出最后的斜杠时,Dir(SourceLoc)的结果是“”(它不搜索目录)。

The frustrating thing is that as I've edited the sub in other ways, the functionality of this code has come and gone. 令人沮丧的是,当我以其他方式编辑sub时,此代码的功能已经过去了。 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. 我已经搜索了在线帮助和ms文章,但似乎没有任何东西可以指出为什么它会继续上下(没有被编辑,除非它停止工作)以及为什么目录路径的格式有时可以用于最终'/'有时没有。

any ideas? 有任何想法吗?

This would open all .xlxs files in that directory son. 这将打开该目录中的所有.xlxs文件。

    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. JLILI Aman找到了将Dir()的结果作为字符串的答案。 Using that combined with the path on Application.Open allows for stable behaviors from the code. 使用它与Application.Open上的路径结合使用可以从代码中获得稳定的行为。

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. 我没有包含推荐的文件扩展名,因为我在一个目录中处理xls,xlsx和xlsm文件。 This code opens all of them. 此代码打开所有这些代码。

Warning - this code will set current file to each file in the directory including non-excel files. 警告 - 此代码将当前文件设置为目录中的每个文件,包括非excel文件。 In my case, I'm only dealing with excel files so that's not a problem. 就我而言,我只处理excel文件,所以这不是问题。

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. 至于为什么会发生这种情况,Application.Open似乎不会接受Dir()的完整对象结果,所以Dir()的返回需要是一个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: 考虑使用包含文件夹和文件属性的VBA的FileSystemObject

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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