从文件夹中选择文件

时间:2018-04-16 18:02:20

标签: excel-vba vba excel

请求帮我解决以下问题。 从我想要选择文件的文件夹中,我可以检索文件名但无法选择文件夹中的文件,只需突出显示该文件即可。我正在使用objFile.Name.Select,但它是通过异常作为对象所需的424

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Desktop\New folder")
For Each objFile In objFolder.Files
    MsgBox objFile.Name
    objFile.Name.Select
   If InStr(objFile.Name, "Document Error") > 0 Then
       Exit For
   End If
Next objFile

1 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Option Explicit

Sub Sample()
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim sFolder As String

    sFolder = "C:\Users\Desktop\New folder"

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

    For Each objFile In objFolder.Files
        MsgBox objFile.Name
        Shell "explorer.exe /select,""" & sFolder & "\" & objFile.Name & """", vbNormalFocus
        If InStr(objFile.Name, "Document Error") > 0 Then Exit For
    Next objFile
End Sub