0

I am trying to handle navigation complete event (ie_NavigateComplete2) in Excel VBA, it is called when the website is not in Local Intranet or Trusted sites of IE Security settings but not otherwise.

enter image description here

here's the code:

Option Explicit

Dim WithEvents ie As InternetExplorer
Sub start_here()
  Set ie = New InternetExplorer
  ie.Visible = True
  ie.Navigate "https://www.example.com"
  While ie.busy
     DoEvents
  Wend
End Sub

Private Sub ie_NavigateComplete2(ByVal pDisp As Object, url As Variant)
    ' this is not called if "https://www.example.com" is in trusted or local sites in IE security setting
    MsgBox "navigatecomplete2"
End Sub

How to circumvent this problem?

braX
  • 11,506
  • 5
  • 20
  • 33
Sadiq Khoja
  • 522
  • 1
  • 5
  • 23

1 Answers1

0

I reproduced the issue when the website is in Local intranet. But it works well when the website is in Trusted sites. I search the issue but don't find why this happens.

You could use DocumentComplete Event as a workaround. It fires when a document is completely loaded and initialized. It can be fired with both Local intranet and Trusted sites:

Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    MsgBox "documentcomplete"
End Sub
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • ie_DocumentComplete is not called when site is in Local Intranet :( – Sadiq Khoja Apr 13 '20 at 13:50
  • I found that `ie_DocumentComplete` won't be fired when **Enable Protected Mode** is unchecked. Please make sure [**Enable Protected Mode** is checked](https://i.stack.imgur.com/4fqBK.png) and restart IE and try again. – Yu Zhou Apr 14 '20 at 02:48
  • can't ask client to change their IE settings, looking something in Excel VBA – Sadiq Khoja Apr 15 '20 at 14:25
  • Then I think you can only use other method to achieve the goal. Do some event after the page is fully loaded. You can put the event you want to excute after `While ie.busy DoEvents Wend`. You could also refer to [this thread](https://stackoverflow.com/questions/33353565/how-do-i-make-vba-wait-for-ie-page-to-load) and [this thread](https://stackoverflow.com/questions/3275515/how-to-wait-until-webbrowser-is-completely-loaded-in-vb-net). – Yu Zhou Apr 16 '20 at 06:40