1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBAでIE操作した時にjavascript:alert()で止まる問題を乗り越える

Last updated at Posted at 2020-07-21

VBAからIE操作してて、HTML上のボタンクリックでjavascript:alert()が表示されるとalert()を手動で閉じるまで、マクロの実行が停止してしまう問題。

調べても非同期がいいらしいということしか見つからず苦労したので答えをメモ書き。

ie.xlsm
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As LongPtr) As LongPtr

'~中略~

Dim IE As InternetExplorer
Set IE = New InternetExplorer

'~中略~

'非同期でid:submitのボタンクリック
IE.document.Script.setTimeout _
        "javascript:document.getElementById(""submit"").click()", 1000
      
Sleep 1000
        
'IEをアクティブにする
SetForegroundWindow IE.hWnd
        
Sleep 1000
        
'Enterキー入力
SendKeys "{ENTER}"
  • 注意点
    • wait入れるために Do While IE.document.ReadyState <> "complete" とかやるとcompleteにならないので止まる。= Sleep等でwaitを入れる。
    • アクティブ~キー入力までの間に、手動で他のウィンドウをアクティブにするとうまく動かない。=裏でマクロ動かしつつ、並行して手作業はしないほうが無難。
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?