IExplore.exeを起こして、そのIEのオブジェクトを正確に捕捉する。(VBA編)
Shell.Windows()の中から、起動したIEのオブジェクトを正確に捕捉するのは、なかなか厄介です。
しかし、VBAでは、Win32APIが使えるので、ウィンドウハンドルとプロセスIDを対応させて、一意に特定できます。
Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef ProcessId As Long) As Long
Function GetWindowProcessId(ByVal hwnd As Long) As Long
Dim ProcessId As Long
GetWindowThreadProcessId hwnd, ProcessId
GetWindowProcessId = ProcessId
End Function
Sub aaa()
Dim Shell As Object
Dim wShell As Object
Dim FullName As String
Dim ProcessId As Long
Dim ie As Object
Set Shell = CreateObject("Shell.Application")
Set wShell = CreateObject("WScript.Shell")
FullName = wShell.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\iexplore.exe\")
ProcessId = VBA.Shell("""" & FullName & """ -nohome",vbHide)
Do
For Each ie In Shell.Windows()
If ie.Visible Then
ElseIf ie.ReadyState <> 0 Then
ElseIf ie.LocationURL <> "" Then
ElseIf GetWindowProcessId(ie.hwnd) = ProcessId Then
Exit Do
End If
Next
Application.Wait [NOW()+"0:00:00.1"]
Loop
ie.Visible = True
'ie.Navigate "about:blank"
'Do While ie.Busy Or ie.ReadyState <> 4
' Application.Wait [now()+"0:00:00.1"]
'Loop
End Sub
« IExplore.exeを起こして、そのIEのオブジェクトを正確に捕捉する。 | トップページ | ブックなしでExcelを起動して、指定フォルダで「ファイルを開く」ダイアログを出す。 »