Enabling two-way communication between javascript and parent WebBroswer class

When I began looking for a way to do this, I thought it was going to be very difficult. What I wanted, was a javascipt method inside a WebBrowser object hosted in a Custom Task Pane, to call a method in the parent object’s class that would then call a method in the web page view of a folder, which would then execute client-side javascript inside that HTML document object. Fortunately, this is a very easy task by utilizing two methods: window.external and HTMLDoc.parentWindow.execScript.

Getting a client-side javascript method to invoke a method inside the parent class is as simple as using:

– In the HTML code:

<a href=”http://blogs.orcsweb.com/ControlPanel/Blogs/posteditor.aspx?SelectedNavItem=NewPost#” onclick=”javascript:window.external.MyMethod(var1, var2);”>Invoke MyMethod</a>

– In parent object’s class

Public Sub MyMethod(ByVal var1 As String, ByVal var2 As String)
MsgBox(“Var1: ” & var1 & “, Var2: ” & var2)
End Sub

In Outlook, if the current folder is set to use a web view instead of the standard folder list, the Application.ActiveExplorer.HTMLDocument reference will contain a valid mshtml.HTMLDocument object (it will throw an exception otherwise). Executing client-side javascript (or vbscript in my case) is as easy as calling the parentWindow.execScript method:

Dim oHTMLDoc As mshtml.HTMLDocument

oHTMLDoc = Application.ActiveExplorer.HTMLDocument

oHTMLDoc.parentWindow.execScript(“ClientSideMethod(‘” & Var1 & “‘,'” & var2 & “‘)”)

The point of doing this is that the web page view of the folder contains an Outlook View Control object, and using the above methods, we are able to set the “Restriction” property of the object from a web page hosted inside a custom task pane.