vbscript >> Communicating Between VB6 and a HTML Document

by WinGuy » Wed, 29 Dec 2004 04:25:41 GMT

I had a need to manipulate the activities of a webpage hosted by the
WebBrowser control from within a Visual Basic project. The requirement was
that VB must be able to communicate with VBScript, and vice versa. This is
how I accomplished the task.

I created a new Visual Basic project and placed the WebBrowser control on
its default form. I initially set that control properties with a name of
WebBrowser1, and as not visible. I placed a command button on top of the
WebBrowser1 control, right-clicked it and sent it to the front and then
right-clicked the WebBrowser1 control and sent it to the back. I set the
form properties to not display a menu bar.

I set the click event handler for the command button to end the program. In
the form unload event is where I destroyed all objects before the VB program
would end.

This code would set the form to entirely fill a 1024x768 screen:
Me.Move 0, 0
Me.Width = 15360: Me.Height = 11520
Me.Visible = True

This code would then initialize that control for full screen and no taskbar
or menus:
WebBrowser1.Left = 10: WebBrowser1.Top = 10
WebBrowser1.Height = Me.Height - 20: WebBrowser1.Width = Me.Width - 20
WebBrowser1.AddressBar = False
WebBrowser1.MenuBar = False
WebBrowser1.StatusBar = False
WebBrowser1.FullScreen = True
WebBrowser1.TheaterMode = True
WebBrowser1.GoHome
WebBrowser1.Visible = True

I defined a global variable named PageReady. Then this code in
WebBrowser1.DocumentComplete:
PageReady = True

Then I created Sub MyTest() and placed this code into it:
WebBrowser1.Navigate MyPath & "Page1.html" 'MyPath is previously defined
PageReady = False
While PageReady = False: DoEvents: DoEvents: DoEvents: Wend 'Page loaded
'Set the text value in the page control named txtWebBrowser!
WebBrowser1.Document.All.Item("txtWebBrowser").Value = "Message from VB6
Code!"
'Get the text value in the page control named txtWebBrowser!
'MsgBox WebBrowser1.Document.All.Item("txtWebBrowser").Value
----------------

That completes the VB part. I then created a webpage called Page1.html,
existing in the MyPath that was specified in the above VB code. The page had
this code:

<HTML>
<HEAD>
<TITLE>Communications Test</TITLE>
<SCRIPT Language="VBScript">
OPTION EXPLICIT
SUB PageLoaded()
txtWebBrowser.Value="READY"
END SUB
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#000000" TEXT="#FFFFFF" LINK="#00FFFF"
VLINK="#FF0000" ALINK="#FF00FF" SCROLL="NO"
ONLOAD="PageLoaded()">
<BASEFONT SIZE=4>
<FONT SIZE=4 FACE="ARIAL">
<CENTER><H1>Theme 1 Page</H1></CENTER>
<TEXTAREA NAME="txtWebBrowser"ROWS=1 COLS=50
style="visibility:visible;position:absolute;top:200;left:40;">
Default Text</TEXTAREA>
</FONT>
</BODY>
</HTML>
----------------------

Launching the webpage by itself will cause the textbox text, initially set
to "Default Text", to be changed to "READY" after the page has loaded.

Launching the page via running the VB project will cause the webpage textbox
text to be changed to "Message from VB6 Code!". I commented out the VB code
that will obtain that textbox content.

This methodology lets code in the webpage communicate back and forth with VB
code via the webpage textbox. This allows synchronizing of code in both of
the applications. I've shown the basic way of providing cross communications
between the applications, but I'll leave it up to others to do something
with it! Maybe someone else will write a complete tutorial and project for
others, rather than just a general explanation as I've done here.
Winguy




vbscript >> Communicating Between VB6 and a HTML Document

by mr unreliable » Wed, 29 Dec 2004 06:02:48 GMT


hi WinGuy,

This info may be more appropriately posted in a vb group.

1. vbScripters normally communicate with a webpage by getting a reference
to the "Document Object Model", which provides access to any value on the
page (not just textboxes).

2. vbScripters will normally make provision for the webpage to communicate
back to the script (and execute script code) by passing a reference to some
routine in the script to the webpage. (You get the reference with
"GetRef").

3. As to waiting for a page to load, vbScripters normally use the
"ReadyState"
property, (rather than "Ready"). oIE.ReadyState = READYSTATE_COMPLETE
is generally regarded as more reliable indicator of the page having finished
loading than Ready = True.

mfg, jw








vbscript >> Communicating Between VB6 and a HTML Document

by WinGuy » Wed, 29 Dec 2004 22:25:25 GMT




Hi, and Happy New Year. Thanks for the comment! I do use the DOM to some
extent, that's how the text box in the webpage gets referenced by code in
the VB project which passes the text READY to VB once the VBScript has
executed. I discovered that the DocumentComplete event for the WebBrowser
class in VB6 could fire before VBScript had activated but the page had
actually loaded regardless. I would get something of the nature "variable
not set" errors reported back to VB under those conditions only. The example
was half VB, half VBScript -- I didn't find a newsgroup that is composed of
both environments, but figured it might be of benefit to someone in this
one. I decided this methodology required a lot less code, as events
themselves didn't have to be coded for.

This is surely the wrong group for this question, but you brought up an
interesting point about the DOM and you just might be able to answer. Maybe
I need to install something so that I can get more Help from my VB6 Object
Browser? I don't know how to include a newly found Library into Object
Explorer, if one exists with the needed Help. I do get Help for native VB6
classes though, but I don't see a Document class listed there.

In VB, my Project Explorer is not giving me any Help at all in the DOM area
for the WebBrowser class. Neither for the InternetExplorer class, nor the
SHDOCVwCTL class. I do see the READYSTATE_COMPLETE member listed for the
SHDOCVwCTL class, but I get no Help for it. Frustrating! Are you aware of a
web site link that doesn't so much go into the advangages and all sorts of
comments but is just a good *reference* (instead of tutorial or general
explanations) very specifically for the Document member of the WebBrowser
class from a VB usage perspective? I've searched and searched without
success. If I right-click and seek help for this member in VB6, Help is not
available, but I didn't include non-VB Libraries this time when I installed
Visual Studio 6. I've not been able to find a comprehensive reference to
this, not even in my old and trusty INet Client SDK, as exposed specifically
by the WebBrowser class. If you don't know the answer right off, then I'll
go ask in a VB group (most .NET stuff will still apply to VB6). If you do
know, thanks MUCH for the link!
Winguy




Communicating Between VB6 and a HTML Document

by mr unreliable » Thu, 30 Dec 2004 01:14:32 GMT

hi WinGuy,

When programming the web browser control, I set a reference to BOTH:

"Microsoft Internet Controls" (shdocvw.dll)

"Microsoft HTML Object Library" (mshtml.tlb)

that should be sufficient.

mfg, jw

p.s. while it's not a "help file", the "DHTML Workshop" on msdn will give
you
a pretty exhaustive explanation and demo code which applies to working with
webpages...




Communicating Between VB6 and a HTML Document

by WinGuy » Thu, 30 Dec 2004 03:08:14 GMT





I only had one of them referenced (mshtml), but now both are ... thanks.

I hope they don't move that Workshop link location again for the next 3
decades or so. That's kind of the problem, the meat is there ... if it can
be found. I accidentally googled a good article that got me "in the
reference articles door" and eventually I ended up with these rather good
bookmarked reference links, most are short on extranious verbiage and long
on syntax. I'll share them, might help someone else ...
Winguy

WebBrowser Object
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reflist_vb.asp

WebBrowser Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reflist_vb.asp

About the DHTML Object Model
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/om/doc_object.asp

DOM Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_document.asp

DHTML Events Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events.asp

DHTML Properties Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties.asp

DHTML Object Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp

DHTML Collections Reference
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/collections.asp




Communicating Between VB6 and a HTML Document

by Fat Cat » Wed, 05 Jan 2005 17:30:27 GMT

Happy New Year Winguy:

You have written the code to set the value of a text, but, have you
discovered
how to set & retrieve the values of Check boxes, and Select Options?

Thanks.










From: WinGuy ( XXXX@XXXXX.COM )
Subject: Communicating Between VB6 and a HTML Document



View this article only
Newsgroups: microsoft.public.scripting.vbscript
Date: 2004-12-28 12:26:03 PST

I had a need to manipulate the activities of a webpage hosted by the
WebBrowser control from within a Visual Basic project. The requirement was
that VB must be able to communicate with VBScript, and vice versa. This is
how I accomplished the task.

I created a new Visual Basic project and placed the WebBrowser control on
its default form. I initially set that control properties with a name of
WebBrowser1, and as not visible. I placed a command button on top of the
WebBrowser1 control, right-clicked it and sent it to the front and then
right-clicked the WebBrowser1 control and sent it to the back. I set the
form properties to not display a menu bar.

I set the click event handler for the command button to end the program. In
the form unload event is where I destroyed all objects before the VB program
would end.

This code would set the form to entirely fill a 1024x768 screen:
Me.Move 0, 0
Me.Width = 15360: Me.Height = 11520
Me.Visible = True

This code would then initialize that control for full screen and no taskbar
or menus:
WebBrowser1.Left = 10: WebBrowser1.Top = 10
WebBrowser1.Height = Me.Height - 20: WebBrowser1.Width = Me.Width - 20
WebBrowser1.AddressBar = False
WebBrowser1.MenuBar = False
WebBrowser1.StatusBar = False
WebBrowser1.FullScreen = True
WebBrowser1.TheaterMode = True
WebBrowser1.GoHome
WebBrowser1.Visible = True

I defined a global variable named PageReady. Then this code in
WebBrowser1.DocumentComplete:
PageReady = True

Then I created Sub MyTest() and placed this code into it:
WebBrowser1.Navigate MyPath & "Page1.html" 'MyPath is previously defined
PageReady = False
While PageReady = False: DoEvents: DoEvents: DoEvents: Wend 'Page loaded
'Set the text value in the page control named txtWebBrowser!
WebBrowser1.Document.All.Item("txtWebBrowser").Value = "Message from VB6
Code!"
'Get the text value in the page control named txtWebBrowser!
'MsgBox WebBrowser1.Document.All.Item("txtWebBrowser").Value
----------------

That completes the VB part. I then created a webpage called Page1.html,Read
the rest of this message... (43 more lines)


Post a follow-up to this message




Communicating Between VB6 and a HTML Document

by EagleEye » Sun, 09 Jan 2005 21:03:29 GMT




Hi folks,

This DEFINITELY should go into the VB area but its in topic with this
thread so I figured what the hey :)

I'm trying to access the DOM Link Objects through the webBrowser control
like thus:

MsgBox whichWin(1).Document.links[1].host

Well, VB doesn't like those square brackets even a little bit and I'm
stumped on how to get around it.

The actual base problem is that I'm trying to intercept onClick events
for every link in the page and I'm trying to intercept them this way to
build an onclick that will point to my own function where I can build my
own windows to display the page in instead of in the default browser.

I must say, that this works:

MsgBox whichWin(whichWinCount - 1).Document.links.Length

But it doesn't like trying to access the array through the DOM accessing
method.

Any thoughts would be greatly appreciated ;)

Dean


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Communicating Between VB6 and a HTML Document

by EagleEye » Mon, 10 Jan 2005 03:33:33 GMT



DUH! Nevermind. Just (Re)discovered beforeNavigate2.

Such a shmuck!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Similar Threads

1. vb6 webbrowser control and html document - Visual Basic/VB

2. Communicate between multiple embeded Excel spreadsheet in a Word document

I have multiple embeded Excel spreadsheet in a Word document.
And I want to run some calculation in one spreadsheet with value from an 
other spreadsheet embeded in this Word document.

Any tip? 


3. convert html string into a html document - VB.Net

4. .NET communicating with VB6 and visa versa

We have some legacy VB6 COM+ code. I am writing some new .NET code that has to interact with the VB6 code. The problem is not only does the VB6 code have to talk to the .NET code but the .NET code has to call the VB6 code. This circular reference seems to be killing me.

I have been using regasm and gacutil on the .NET assembly (it has a strong name) and tlbimp.exe on the VB6 code and then regasm and gacutil on the generated dll.

If the .NET assembly compiles and registers I get this error message when I compile the VB6 code

LINK : fatal error LNK1104: cannot open file "C:\blah\Prog_VB6.dll"

What is the process to build these two assemblies that both link to each other? Am I going to have to use late binding (ideally in the VB6 code as that will be replaced eventually)?