2

I am trying to create an instance of Internet Explorer from a WPF application, load a saved local file, and do some further processing once the file is loaded. However, although the file is visible in the Internet Explorer window, the DocumentComplete event never fires:

'static field
Dim iex As ShDocVw.InternetExplorer

Public Sub DoStuff()
    Dim path = "c:\test.htm"
    iex = New SHDocVw.InternetExplorer
    iex.Visible = True
    AddHandler iex.DocumentComplete, Sub(o As Object, ByRef url As Object)
            'This code is never executed
            Dim i = 5
        End Sub
    iex.Navigate2(path)
End Sub

When I navigate to a non-local URL (e.g. http://www.google.com) the DocumentComplete event does fire.

The same behavior exists for the NavigateComplete2 event.

I tried using a class member method instead of a lambda expression (maybe the lambda expression is going out of scope once the method exits?) using both AddressOf and Handles, but that didn't help.

What do I have to do to have the DocumentComplete event fire?

(NB: The page has no frames.)

Update

This code is being used in a class library, and I therefore cannot use the WebBrowser control, as it cannot be instantiated in code.

As SimonMourier points out in the comments, a WebBrowser can be instantiated in code:

Dim wb = New WebBrowser
AddHandler wb.LoadCompleted, Sub(s, e)
    Dim i = 5
End Sub
wb.Navigate(path)

Although the LoadCompleted event still doesn't fire, the Navigated event does, and it appears to be sufficient for my purposes. (Apparently the WebBrowser has to be visible in order for LoadCompleted to fire -- see here and here -- and since I am not using the WebBrowser in the context of a window, I don't think this is even possible in my case.)

Community
  • 1
  • 1
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • Why don't you use the WebBrowser control? – Simon Mourier Jul 16 '14 at 07:09
  • @SimonMourier Don't I need to define/instantiate the WebBrowser in the context of a WPF Window? Also, from your experience, would a WebBrowser not have this problem? – Zev Spitz Jul 16 '14 at 09:43
  • Well, the WebBrowser is designed for exactly this scenario, and yes, it supports (most) IE events fine. You only need to interop if you need to access native interfaces (like IHtmlDocument2, etc.). I suggest you simply try it anyway. – Simon Mourier Jul 16 '14 at 10:44
  • @SimonMourier This is code in a class library, and I don't think I have any way to instantiate a WebBrowser from within a class library. – Zev Spitz Jul 16 '14 at 11:11
  • WebBrowser is a class you can create anywhere – Simon Mourier Jul 16 '14 at 12:43
  • @SimonMourier You said it first, so if you want the bounty, please post as an asnwer. Otherwise I'll award the bounty to Nitin Joshi. – Zev Spitz Jul 17 '14 at 16:45

3 Answers3

1

Instead of using ShDocVw.InternetExplorer you can use WebBrowser control provided by WPF:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <WebBrowser x:Name="webBrowser" Visibility="Visible" />
    </Grid>
</Window>



Class MainWindow
    Public Sub DoStuff()
        Dim path = New Uri("c:\test.htm")
        AddHandler webBrowser.LoadCompleted, Sub(sender As Object, e As System.Windows.Navigation.NavigationEventArgs)
                                                 Dim i = 5
                                             End Sub
        webBrowser.Navigate(path)
    End Sub

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        DoStuff()
    End Sub
End Class

enter image description here

Nitin Joshi
  • 1,638
  • 1
  • 14
  • 17
  • When I tried with your code, it works perfectly. `DocumentCompleted` is being called when I open a local .htm file. Check if you have required permissions on the file. Check [this](http://social.msdn.microsoft.com/Forums/ie/en-US/b35b9902-c0fa-4ce5-b3e1-c74d840fe81c/only-documentcomplete-not-fired-on-some-computers?forum=ieextensiondevelopment) and [this](http://social.msdn.microsoft.com/Forums/vstudio/en-US/d43931ae-4d9e-4ad6-bde4-b7c5abb8a6f0/documentcomplete-event-is-not-fired-when-loading-local-html-page?forum=csharpgeneral) links, if these can help you. – Nitin Joshi Jul 16 '14 at 12:55
  • 1
    What permissions could I be missing if I see the page in the Internet Explorer window? – Zev Spitz Jul 16 '14 at 20:23
1

You should use the out-of-the-box standard WebBrowser Control that ships with WPF (there is another one for Winforms apps). It has all the basic events directly supported.

Should you miss some Winforms feature like IsWebBrowserContextMenuEnabled or ScriptErrorsSuppressed, I suggest you refer to my answer in this question on SO: How to deactivate "right click" on WPF Webbrowser Control?

You only need interop in these special cases or if you need to get ahold on the native underlying IE's Document Object Model (DOM), interfaces like IHTMLDocument2, etc.

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
1

There is no need to use WebBrowser Control to get this issue resolved.

I have also faced this issue and it happens due to the access privileges. Run your application with Admin privileges and it will be fine.

To debug try running your Visual Studio as Administrator and then test, The DocumentComplete event will be fired.

Update1:

In case of non-admin application if you can manage to get the internet explorer started with Admin privileges then also you can work with it using your non-admin application. Simple start a Internet Explorer process with admin privileges.

Then you can hook it using this code

   For Each IE As InternetExplorer In New SHDocVw.ShellWindows
        If IE.FullName.ToLower.Contains("iexplore") And IE.LocationURL <> "" Then
            'Capture IE here
        End If
    Next
prem
  • 3,348
  • 1
  • 25
  • 57