Option Explicit
'DeclareステートメントにPtrSafe属性を追加;64ビット版Excel対応のため
Declare PtrSafe Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, ByVal lpszWindow As String) As Long
'ダイアログウィンドウを探す
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
'Postmassage
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Const WM_COMMAND As Long = &H111&
'***************************************************************
Sub hoge()
===省略===
Sleep 1000
'Webページからのポップアップをクリック
Dim hwnd As LongPtr
hwnd = FindWindow("#32770", "Web ページからのメッセージ")
PostMessage hwnd, WM_COMMAND, vbOK, 0& 'システムダイアログのOKボタンを押す
End sub
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Const WM_COMMAND As Long = &H111&
________________________________________________________________________________________
Sub test()
Dim HandleIE As InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ie As InternetExplorerMedium
Dim objButton As HTMLInputButtonElement
'(省略)
Set ie = IEオブジェクト
For Each objButton In ie.document.getElementsByTagName("foo")
If objButton.Value = "piyo" Then
objButton.click() 'VBAのclickメソッドでクリックすると、"WEBページからのメッセージ"が出現した時、システム側に制御を持っていかれてしまう
' 制御はシステム側に残ったままなので、↓以下のコードはいつまで経っても実行されない
Sleep 1000
Dim hwnd As LongPtr
hwnd = FindWindow("#32770", "Web ページからのメッセージ")
PostMessage hwnd, WM_COMMAND, vbOK, 0&
End Sub
###動作OK
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Const WM_COMMAND As Long = &H111&
________________________________________________________________________________________
Sub test()
Dim HandleIE As InternetExplorerMedium
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ie As InternetExplorerMedium
Dim objButton As HTMLInputButtonElement
'(省略)
Set ie = IEオブジェクト
For Each objButton In ie.document.getElementsByTagName("foo")
If objButton.Value = "piyo" Then
'対象のボタンを取得後、VBAではなく、javascriptでクリックする。そうすることで、制御はVBAに残ったままになり、以下のコードが実行される。
ie.document.Script.setTimeout "javascript:document.getElementById('hage').click()", 200
Sleep 1000
hwnd = FindWindow("#32770", "Web ページからのメッセージ")
If hwnd <> 0 Then
Debug.Print hwnd, ie.hwnd
PostMessage hwnd, WM_COMMAND, vbOK, 0
End If
Exit For
End If
Next