调用shell - 只知道文件名的一部分

时间:2016-04-27 13:27:14

标签: vba

我的宏创建名为year.number.description(2016.185.Hello)的文件夹。

问题是此名称可能会更改,例如,有些日后有人将其重命名为2016.185.Hi

在同一个宏中,有一个打开该文件夹的选项。

我认为添加“。*”会起作用,但似乎没有。

Private Sub cmdNumero_Click()

Dim Carpeta As String
Año = Me.txtAny.Value
Numero = Me.txtNumero.Value

Carpeta = "C:\Folder1\Folder2" & "\" & Año & "." & Numero & ".*"

Call Shell("explorer.exe " & Carpeta, vbNormalFocus)

End Sub

感谢您的关注

1 个答案:

答案 0 :(得分:0)

您可以使用辅助函数来获取实际文件夹路径..如下所示:

Function GetSubFolderByPartial(sRootPath As String, sSub As String) As String
    Dim oFso As Object
    Dim oTopFolder As Object
    Dim oSubs As Object
    Dim oFolder As Object

    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set oTopFolder = oFso.GetFolder(sRootPath)
    Set oSubs = oTopFolder.SubFolders
    For Each oFolder In oSubs
        If oFolder.Name Like sSub & "*" Then
            GetSubFolderByPartial = oFolder.Path
            Set oFso = Nothing
            Exit Function
        End If
    Next
End Function

所以,你可以简单地这样称呼它:

Carpeta = GetSubFolderByPartial("C:\Folder1\Folder2", Año & "." & Numero & ".")
If Len(Carpeta) > 0 Then
    Call Shell("explorer.exe " & Carpeta, vbNormalFocus)
End If

希望有所帮助