WebBrowser Control (VB5, VB6) & window.external. Possible? 
Author Message
 WebBrowser Control (VB5, VB6) & window.external. Possible?

I'm trying to communicate between a WebBrowser control and its VB5/VB6
container. I know that in VS2005 it's as easy as defining a public sub
in the application:

   Public Sub doSomething()
     ' Do Something!
   End Sub

and in the document call

   window.external.doSomething

This apparently does not work in VB5/VB6. Is there any way to do this or
something similar?

Thanks in advance.



Sun, 09 Jan 2011 08:34:53 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?


Quote:
> I'm trying to communicate between a WebBrowser control and its VB5/VB6
> container.

Here's something that may get you started....  (VB6)

http://msdn.microsoft.com/en-us/library/aa752045.aspx

LFS



Sun, 09 Jan 2011 19:48:07 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
  It might help if you explain what exactly you're
trying to do. The WB events are available in the
form code without any extra effort. If you want
the document events in order to time calls to
form-related functions, first declare in the form:

Private WithEvents Doc As mshtml.HTMLDocument

(Requires a reference to mshtml.dll.)

  Then in WB.document_complete do something
like the following:

Private Sub WB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  Set Doc = Nothing
  IsDoc = False
    If Not WB.document Is Nothing Then
        Set Doc = WB.document
        IsDoc = True
    End If
End Sub

   In that example IsDoc is a global variable. Since
the document object is gone with each page load,
in order to avoid referencing an empty doc object
it helps to keep track of it. I find it works well to
do like above and then in WB_BeforeNavigate set
IsDoc = False. Using that method means that you
always get your relevant Doc events and that you
can also safely reference the doc object by using:

If IsDoc = True then....

Quote:
> I'm trying to communicate between a WebBrowser control and its VB5/VB6
> container. I know that in VS2005 it's as easy as defining a public sub
> in the application:

>    Public Sub doSomething()
>      ' Do Something!
>    End Sub

> and in the document call

>    window.external.doSomething

> This apparently does not work in VB5/VB6. Is there any way to do this or
> something similar?

> Thanks in advance.



Sun, 09 Jan 2011 21:25:17 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
What I'm trying to do is the following. I have a WebBrowser control
hosted inside a VB5/VB6 form. The WebBrowser control loads a HTML page
and in that HTML page I want to be able to query the hosting VB5/VB6
application for some variables through JavaScript (or any other means).

For instance, in the HTML page:

   <SCRIPT>
     function showVersionNumber() {
       var cn = window.external.getVersionNumber();
       alert( cn );
     }
   </SCRIPT>

Then in the VB5/VB6 application:

   Public Function getVersionNumber() as String
     getVersionNumber = App.Major & "." & App.Minor
   End Function

This is just an example, I'm not really trying to get the version
number, I just want to be able to query the hosting VB5/VB6 application.
I know I can simply "inject" variables into the HTML code, but the
variables I want to query are dynamic so their values must be generated
at the moment of the query.

Thanks again in advance.

Quote:

>   It might help if you explain what exactly you're
> trying to do. The WB events are available in the
> form code without any extra effort. If you want
> the document events in order to time calls to
> form-related functions, first declare in the form:

> Private WithEvents Doc As mshtml.HTMLDocument

> (Requires a reference to mshtml.dll.)

>   Then in WB.document_complete do something
> like the following:

> Private Sub WB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
>   Set Doc = Nothing
>   IsDoc = False
>     If Not WB.document Is Nothing Then
>         Set Doc = WB.document
>         IsDoc = True
>     End If
> End Sub

>    In that example IsDoc is a global variable. Since
> the document object is gone with each page load,
> in order to avoid referencing an empty doc object
> it helps to keep track of it. I find it works well to
> do like above and then in WB_BeforeNavigate set
> IsDoc = False. Using that method means that you
> always get your relevant Doc events and that you
> can also safely reference the doc object by using:

> If IsDoc = True then....

>> I'm trying to communicate between a WebBrowser control and its VB5/VB6
>> container. I know that in VS2005 it's as easy as defining a public sub
>> in the application:

>>    Public Sub doSomething()
>>      ' Do Something!
>>    End Sub

>> and in the document call

>>    window.external.doSomething

>> This apparently does not work in VB5/VB6. Is there any way to do this or
>> something similar?

>> Thanks in advance.



Sun, 09 Jan 2011 22:46:18 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?


Quote:
> What I'm trying to do is the following. I have a WebBrowser control
> hosted inside a VB5/VB6 form. The WebBrowser control loads a HTML page
> and in that HTML page I want to be able to query the hosting VB5/VB6
> application for some variables through JavaScript (or any other
> means).

> For instance, in the HTML page:

>    <SCRIPT>
>      function showVersionNumber() {
>        var cn = window.external.getVersionNumber();
>        alert( cn );
>      }
>    </SCRIPT>

> Then in the VB5/VB6 application:

>    Public Function getVersionNumber() as String
>      getVersionNumber = App.Major & "." & App.Minor
>    End Function

> This is just an example, I'm not really trying to get the version
> number, I just want to be able to query the hosting VB5/VB6
> application.

It may be better to state something that you actually are trying to
retrieve.


Sun, 09 Jan 2011 23:47:44 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
Hi,

The "easy" way is to use VS2005.  Create an ActiveX control using the Com
Interrop toolkit that wraps the VS2005 browser control in code that can use
the syntax that you describe.  Add additional public routines (and events,
as needed) to communicate with your legacy VB6 application.  You employ this
ActiveX control in your VB6 app, just about like any other.

I've done what I've described above -- and it works well.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.



Mon, 10 Jan 2011 00:41:06 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
  I've never run across this before. Apparently what
you want to do is possible but awkward. MS claims it's
explained in an article named "Reusing the Web Browser
Control and MSHTML", but I haven't been able to find
an actual link to that article. Other links:

Sample C++ code here:
http://ehsanakhgari.org/article/visual-c/extending-webbrowser-dom

See last question here:
http://msdn.microsoft.com/en-us/magazine/cc302075.aspx

  It sounds like you need to write a C++ DLL or write a VB
COM object using a typelib to implement IDocHostUIHandler
with its GetExternal method.

  Maybe you can use the C++ code. Or, as DanS suggested,
post the details of what you're actually trying to accomplish
if you don't want to tackle the external code but do want
to find a solution. Maybe an external object is handy, but I
can't think of much use that it might have. Since you don't
want your remote webpage controlling the WB parent form
to access the local file system, it's hard to think of many things
that you *would* want the page to be able to retrieve.



Mon, 10 Jan 2011 00:54:05 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
 > It may be better to state something that you actually are trying to
 > retrieve.

Why would that matter? I just want to exchange data between the
JavaScript script and the VB application. It could be data from a
database, it could be data retrieved from yet another computer in the
intranet, or it could be something as simple as a few pre-defined
constants. I just want the JavaScript to be able to query the host VB app.

Quote:



>> What I'm trying to do is the following. I have a WebBrowser control
>> hosted inside a VB5/VB6 form. The WebBrowser control loads a HTML page
>> and in that HTML page I want to be able to query the hosting VB5/VB6
>> application for some variables through JavaScript (or any other
>> means).

>> For instance, in the HTML page:

>>    <SCRIPT>
>>      function showVersionNumber() {
>>        var cn = window.external.getVersionNumber();
>>        alert( cn );
>>      }
>>    </SCRIPT>

>> Then in the VB5/VB6 application:

>>    Public Function getVersionNumber() as String
>>      getVersionNumber = App.Major & "." & App.Minor
>>    End Function

>> This is just an example, I'm not really trying to get the version
>> number, I just want to be able to query the hosting VB5/VB6
>> application.

> It may be better to state something that you actually are trying to
> retrieve.



Mon, 10 Jan 2011 02:45:18 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?


Quote:
> Why would that matter? I just want to exchange data between the
> JavaScript script and the VB application. It could be data from a
> database, it could be data retrieved from yet another computer in the
> intranet, or it could be something as simple as a few pre-defined
> constants. I just want the JavaScript to be able to query the host VB app.

As the link I pointed you to shows, you can create a class to respond to
events on the webpage.  In your case you might add a hidden textbox to
the webpage and respond to a property change on that object.  That event
will give you the opportunity to change the value of the hidden textbox
which you can use to pass back the value you want in your script.

For a simple example, the button on a webpage is updated with text
from a textbox.  To a new form add a textbox and webbrowser
control.  Add a class to the project and add a reference to the
Microsoft HTML Object Library.  Then paste the code below in
their respective modules and try it out....

LFS

'[ Form1 code ]
Option Explicit
Private ButtonSync As Class1

Private Sub Form_Load()
   Show
   WebBrowser1.Navigate2 "about:blank"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   With pDisp.Document
     .open
     .write "Button shows Textbox contents.<br><input type='button' name='sync' value='Click Me'>"
     .Close
   End With
   Set ButtonSync = New Class1
   Set ButtonSync.HTMLButton = pDisp.Document.All("Sync")
End Sub

'[ Class1 code ]
Option Explicit

Public WithEvents HTMLButton As HTMLInputElement

Private Function HTMLButton_onclick() As Boolean
  HTMLButton.Value = Form1.Text1.Text
End Function



Mon, 10 Jan 2011 03:46:24 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?

Quote:
> > It may be better to state something that you actually are trying to
>  > retrieve.

> Why would that matter?

   It matters because, as I pointed out 2 hours earlier,
what you want to do is somewhat involved and may not
be necessary. I'm wondering if you might have misunderstood
something. (Or maybe I did. :)  For a remote webpage to be
able to ask the browser to access a database on an intranet
doesn't make sense. In normal usage, when accessing the
parent as a navigator object, the only things available are
information properties about the browser: name, useragent,
version number, etc. What you're asking to do sounds more
like the equivalent of letting a webpage load the FileSystemObject.
Quote:
> I just want to exchange data between the
> JavaScript script and the VB application. It could be data from a
> database, it could be data retrieved from yet another computer in the
> intranet, or it could be something as simple as a few pre-defined
> constants. I just want the JavaScript to be able to query the host VB app.



Mon, 10 Jan 2011 05:36:48 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
There isn't anything specific I want to retrieve (yet). I'm just trying
to figure out if this approach will work.

Let me give an example of how I want to use this.

First of all, I have a VB5/VB6 application that hosts a WebBrowser
Control. This Control navigates to a fixed page on the intranet (or
internet for that matter).

Suppose this page needs to display the client's computername, do some
calculations with the free diskspace and use some data from a device
attached to the client's computer.

All this information is available to the VB application, but not to the
WebBrowser Control. So I want the page to be able to "ask" the hosting
VB app for this information. For instance like this:

   <SCRIPT Language=JavaScript>
     function getInformation() {
       computerName = window.external.getComputerName();
       freeSpace = window.external.getFreeDiskSpace();
       importantData = window.external.getDataFromDevice();
       // Do whatever I want to do with this data
     }
   <SCRIPT>

These three functions (getComputerName(), getFreeDiskSpace() and
getData()) should all be functions in the VB app.

I know that in VS2005/VS2008 this is as easy as setting the WebBrowser
Control's ObjectForScripting property:

   webbrowser1.ObjectForScripting = me;

But there's no such property in VB5/VB6.

Quote:

>>> It may be better to state something that you actually are trying to
>>  > retrieve.

>> Why would that matter?

>    It matters because, as I pointed out 2 hours earlier,
> what you want to do is somewhat involved and may not
> be necessary. I'm wondering if you might have misunderstood
> something. (Or maybe I did. :)  For a remote webpage to be
> able to ask the browser to access a database on an intranet
> doesn't make sense. In normal usage, when accessing the
> parent as a navigator object, the only things available are
> information properties about the browser: name, useragent,
> version number, etc. What you're asking to do sounds more
> like the equivalent of letting a webpage load the FileSystemObject.

>> I just want to exchange data between the
>> JavaScript script and the VB application. It could be data from a
>> database, it could be data retrieved from yet another computer in the
>> intranet, or it could be something as simple as a few pre-defined
>> constants. I just want the JavaScript to be able to query the host VB app.



Mon, 10 Jan 2011 06:05:26 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
Forgot to add:

I know I can easily "inject" data into the webpage for use in Javascript
like this:

VB:

   Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL
As Variant)
     Dim doc As HTMLDocument
     Set doc = WebBrowser1.Document
     doc.getElementById("ComputerName").getAttribute("value") =
getComputerName()

   End Sub

HTML:

   <INPUT ID="ComputerName" TYPE="HIDDEN">

In Javascript, I can then simply read the contents of the ComputerName
field.

But this is cumbersome. For dynamic data, I would need to inject this
data in intervals which uses up unnecessary resources. And if there's a
lot of possible data to retrieve, I would need to inject all the
possible data.

Quote:

>>> It may be better to state something that you actually are trying to
>>  > retrieve.

>> Why would that matter?

>    It matters because, as I pointed out 2 hours earlier,
> what you want to do is somewhat involved and may not
> be necessary. I'm wondering if you might have misunderstood
> something. (Or maybe I did. :)  For a remote webpage to be
> able to ask the browser to access a database on an intranet
> doesn't make sense. In normal usage, when accessing the
> parent as a navigator object, the only things available are
> information properties about the browser: name, useragent,
> version number, etc. What you're asking to do sounds more
> like the equivalent of letting a webpage load the FileSystemObject.

>> I just want to exchange data between the
>> JavaScript script and the VB application. It could be data from a
>> database, it could be data retrieved from yet another computer in the
>> intranet, or it could be something as simple as a few pre-defined
>> constants. I just want the JavaScript to be able to query the host VB app.



Mon, 10 Jan 2011 06:13:23 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
I'm sorry, I'm making a mess of things. The line

doc.getElementById("ComputerName").getAttribute("value") = getComputerName()

should actually read:

doc.getElementById("ComputerName").setAttribute "value", getComputerName()

Quote:

> Forgot to add:

> I know I can easily "inject" data into the webpage for use in Javascript
> like this:

> VB:

>   Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As
> Variant)
>     Dim doc As HTMLDocument
>     Set doc = WebBrowser1.Document
>     doc.getElementById("ComputerName").getAttribute("value") =
> getComputerName()

>   End Sub

> HTML:

>   <INPUT ID="ComputerName" TYPE="HIDDEN">

> In Javascript, I can then simply read the contents of the ComputerName
> field.

> But this is cumbersome. For dynamic data, I would need to inject this
> data in intervals which uses up unnecessary resources. And if there's a
> lot of possible data to retrieve, I would need to inject all the
> possible data.


>>>> It may be better to state something that you actually are trying to
>>>  > retrieve.

>>> Why would that matter?

>>    It matters because, as I pointed out 2 hours earlier,
>> what you want to do is somewhat involved and may not
>> be necessary. I'm wondering if you might have misunderstood
>> something. (Or maybe I did. :)  For a remote webpage to be
>> able to ask the browser to access a database on an intranet
>> doesn't make sense. In normal usage, when accessing the
>> parent as a navigator object, the only things available are
>> information properties about the browser: name, useragent,
>> version number, etc. What you're asking to do sounds more
>> like the equivalent of letting a webpage load the FileSystemObject.

>>> I just want to exchange data between the
>>> JavaScript script and the VB application. It could be data from a
>>> database, it could be data retrieved from yet another computer in the
>>> intranet, or it could be something as simple as a few pre-defined
>>> constants. I just want the JavaScript to be able to query the host VB
>>> app.



Mon, 10 Jan 2011 06:19:22 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?

Quote:
> I'm sorry, I'm making a mess of things. The line

> doc.getElementById("ComputerName").getAttribute("value") =
getComputerName()

> should actually read:

> doc.getElementById("ComputerName").setAttribute "value", getComputerName()

  The "injecting" method seems much easier to me,
but I think I get what you mean, in any case:
You want the program itself to be a scriptable object.

  According to the links I posted earlier you can do that
by implementing IDocHostUIHandler, which is one of
the interfaces available in Eduardo Morcillo's olelib
typelib. I don't have any experience with that myself.
Sorry. Maybe someone else will pipe up with a better
answer. Although when I went searching for information
about this I didn't find any examples of someone having
done it in VB.



Mon, 10 Jan 2011 08:47:34 GMT  
 WebBrowser Control (VB5, VB6) & window.external. Possible?
I'm sorry, I hadn't read your reply right. I thought you just answered
"use VS2005 instead of VB5/VB6". But your solution might be the best of
all. I will try that out.
Quote:

> Hi,

> The "easy" way is to use VS2005.  Create an ActiveX control using the Com
> Interrop toolkit that wraps the VS2005 browser control in code that can use
> the syntax that you describe.  Add additional public routines (and events,
> as needed) to communicate with your legacy VB6 application.  You employ this
> ActiveX control in your VB6 app, just about like any other.

> I've done what I've described above -- and it works well.



Mon, 10 Jan 2011 15:52:47 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Passing window.external to OCX (VB6) (or way of retrieving window.external like in C++ ServiceProvider)

2. WebBrowser without WebBrowser Control in VB6 ?

3. Using window.external.myobejct in VB5

4. VB5 Pro & VB6 Ent Kill VB5 Programs

5. WebBrowser control on VB6 control???

6. Webbrowser control and secure sites - possible?

7. WebBrowser control - possible to print contents?

8. Is this possible: vb6 & Outlook

9. Using Data Control & DBGrid with external file

10. VB6 Internal DLL Variable can not be set to external activex control in IE

11. BUG: VB6 Fails to debug VB-Made ActiveX control with external program on Windows2000\XP

12. GURU needed: VB6 webbrowser: Detect Navigation to local File system -- suppress new window

 

 
Powered by phpBB® Forum Software