Last active
January 8, 2021 02:28
-
-
Save kumatti1/2222dcaabf4f14b59d6e to your computer and use it in GitHub Desktop.
Explorerのファイル選択
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Private Declare Function ILCreateFromPathW& Lib "Shell32.dll" (ByVal pszPath&) | |
Private Declare Sub ILFree Lib "Shell32.dll" (ByVal pidl&) | |
Private Declare Function ILGetSize& Lib "Shell32.dll" (ByVal pidl&) | |
Private Declare _ | |
Function InitVariantFromBuffer Lib "propsys.dll" ( _ | |
ByVal pv As LongPtr, _ | |
ByVal cb As Long, _ | |
ByRef pvar As Variant _ | |
) As Long | |
Private Declare PtrSafe Function SHGetIDispatchForFolder Lib "shdocvw.dll" (ByVal pidl As Long, arg2 As Any) As Long | |
Sub test() | |
Dim obj As Object | |
Dim IE As Object | |
Dim tmp&, pidl&, v, strPath$, hr&, strFile$ | |
strPath = "D:\" | |
strFile = "test.txt" | |
'パスからアイテムIDリストへのポインタを得る | |
pidl = ILCreateFromPathW(StrPtr(strPath)) | |
hr = InitVariantFromBuffer(pidl, ILGetSize(pidl), v) | |
'Explorerをパスで検索 | |
Set IE = CreateObject("Shell.Application").Windows.findwindowSW(v, Empty, 1&, 0, 1&) | |
If IE Is Nothing Then | |
'Explorer起動 | |
hr = SHGetIDispatchForFolder(pidl, IE) | |
IE.Visible = True | |
While IE.busy Or IE.ReadyState <> 4 | |
DoEvents | |
Wend | |
'ファイル選択 | |
For Each v In IE.Document.Folder.Items | |
If v.Name = strFile Then | |
IE.Document.SelectItem v, 1 | |
Exit For | |
End If | |
Next | |
Else | |
While IE.busy Or IE.ReadyState <> 4 | |
DoEvents | |
Wend | |
'既に選択済みの要素を選択解除 | |
Set obj = IE.Document.FocusedItem | |
If obj.Name <> strFile Then | |
IE.Document.SelectItem obj, 0 | |
End If | |
'ファイル選択 | |
For Each v In IE.Document.Folder.Items | |
If v.Name = strFile Then | |
IE.Document.SelectItem v, 1 | |
Exit For | |
End If | |
Next | |
IE.Visible = True | |
End If | |
'pidl解放 | |
ILFree pidl | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment