AW: Ordner öffnen mit teilw. bekanntem Namen
02.03.2021 12:12:11
Nepumuk
Hallo Stefan,
teste mal. Der Ordner der gesucht werden soll enthält den Namen Test.
Option Explicit
Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
ByVal hwnd As LongPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As LongPtr
Private Const SW_SHOWNORMAL As Long = 1
Public Sub Test()
Dim strFolder As String
If SearchFolder("Test", strFolder) Then
Call ShellExecuteA(0, "OPEN", strFolder, vbNullString, vbNullString, SW_SHOWNORMAL)
Else
Call MsgBox("Ordner nicht gefunden.", vbExclamation, "Hinweis")
End If
End Sub
Private Function SearchFolder(ByVal pvstrSearchText As String, ByRef pvstrFolder As String) As Boolean
Const FOLDER_PATH As String = "G:\Eigene Dateien\" 'Ordner in dem gesucht werden soll
Dim strFolder As String, strPath As String
Dim astrFolders() As String
Dim ialngIndex1 As Long, ialngIndex2 As Long
Dim blnFound As Boolean
strPath = FOLDER_PATH
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
Do
strFolder = Dir$(PathName:=strPath & "*", Attributes:=vbDirectory)
Do Until strFolder = vbNullString
If strFolder <> "." And strFolder <> ".." Then
If GetAttr(PathName:=strPath & strFolder) And vbDirectory Then
If InStr(1, strFolder, pvstrSearchText, vbTextCompare) > 0 Then
SearchFolder = True
pvstrFolder = strPath & strFolder & "\"
blnFound = True
Exit Do
Else
Redim Preserve astrFolders(0 To ialngIndex1)
astrFolders(ialngIndex1) = strPath & strFolder & "\"
ialngIndex1 = ialngIndex1 + 1
End If
End If
End If
strFolder = Dir$
Loop
If blnFound Then Exit Do
If ialngIndex1 = ialngIndex2 Then Exit Do
strPath = astrFolders(ialngIndex2)
ialngIndex2 = ialngIndex2 + 1
Loop
End Function
Gruß
Nepumuk