Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript Button 2

Status
Not open for further replies.

tfcrip

Technical User
Joined
Nov 27, 2003
Messages
2
Location
NL
Hello,

Was wondering if anyone could help. I need to display some text and a button, using a vbs.file. I'm doing it this way inorder that I can use HTML and also specifiy a paired down version of Explorer(essentially a simple window that displays HTML).

To do this I use.

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 800
objExplorer.Height = 570
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1

Set objDocument = objExplorer.Document
objDocument.Open
Set objExplorer = WScript.CreateObject("InternetExplorer.Application", "IE_")

objDocument.Writeln&quot;<html><head><title>CCN Announcement</title></head>&quot;
objDocument.Writeln&quot;<body bgcolor=#5959ab>&quot;
objDocument.Writeln&quot;<h2>Communication Details</h2>&quot;
objDocument.Writeln&quot;<Br>&quot;
objDocument.Writeln&quot;<Br>&quot;
objDocument.Writeln&quot;<input align=centre id=cmdOK type=Button name=cmdOK value=OK>&quot;

objDocument.Writeln&quot;<body><html>&quot;


objDocument.Writeln&quot;<script language=VBScript>&quot;
Sub cmdOK_onClick()
WScript.Quit
End Sub

Sub IE_onQuit()
Wscript.Quit
End Sub

Unfortunately the button doesn't work. I'm certain it should be simple but I'm completely new to this so have no idea what I'm doing wrong.

Any ideas appreciated.

Thanks

T
 
Try something like this in a .vbs file:
Code:
Set IE=WScript.CreateObject(&quot;InternetExplorer.Application&quot;, &quot;IE_&quot;)
IE.Navigate &quot;about:blank&quot;  
IE.ToolBar=0
IE.StatusBar=0
IE.Width=800
IE.Height=570
IE.Left=0
IE.Top=0
IE.Visible=1
Set D=IE.Document    
D.Open  
D.Writeln &quot;<html><head><title>CCN Announcement</title></head>&quot;
D.Writeln &quot;<body bgcolor=#5959ab>&quot;
D.Writeln &quot;<h2>Communication Details</h2>&quot;
D.Writeln &quot;<Br>&quot;
D.Writeln &quot;<Br><form name=form1>&quot;
D.Writeln &quot;<input id=cmdOK type=Button name=cmdOK value=OK onclick='window.Close()'>&quot;
D.Writeln &quot;</form></body></html>&quot;
While True
  WScript.Sleep 1000
Wend
Sub IE_onQuit()
   WScript.Echo &quot;IE closed&quot;: Wscript.Quit
End Sub

Hope This Help
PH.
 
Hmm...

You don't need a <form> unless you intend to submit it.

Beyond this, while I don't know how you plan to use it I think you are attempting to reinvent the HTML Application (HTA). This is a really slick way to do desktop scripting that is intended to interact with the user via a rich interface.

Instead of having the security limitations of an HTML page, an HTA is intended to be a real desktop application and isn't limited by many of the security constraints IE imposes on web pages. An HTA can have an icon associated with it, can freely use ActiveX, allows cross-frame scripting, and can even be set to allow only one copy to run at a time.

Here's an example:

sample.hta
Code:
<html>
  <head>
    <title>CCN Announcement</title>
    <hta:application
      id=&quot;objHTA&quot;
      applicationName=&quot;CCN Announcement&quot;
      border=&quot;thin&quot; borderStyle=&quot;normal&quot;
      caption=&quot;yes&quot;
      contextMenu=&quot;no&quot;
      icon=&quot;&quot;
      innerBorder=&quot;yes&quot; borderStyle=&quot;normal&quot;
      maximizeButton=&quot;no&quot;
      minimizeButton=&quot;yes&quot;
      navigable=&quot;no&quot;
      scroll=&quot;no&quot; scrollFlat=&quot;no&quot;
      selection=&quot;no&quot;
      showInTaskbar=&quot;yes&quot;
      singleInstance=&quot;yes&quot;
      sysMenu=&quot;yes&quot;
      version=&quot;0.0&quot;
      windowState=&quot;normal&quot; />
    <script language=VBScript>
      Option Explicit

      Sub cmdOK_onclick()
        window.close
      End Sub

      Sub window_onload()
        window.moveTo 0, 0
        window.resizeTo 800, 570
      End Sub
    </script>
  </head>
  <body bgcolor=#5959ab>
    <h2>Communication Details</h2>
    <br>
    <br>
    <input id=cmdOK type=button value=OK>
  </body>
</html>
If this can do what you want it can be a whole lot less messy than trying to use IE via puppet strings from a WSH script. An HTA is event-driven like any DHTML page (or say, a typical VB program). This means no &quot;sleep&quot; loops in order to operate as an event handler - that's native behavior here.

Actually, the lack of the
Code:
WScript.Sleep
method is one of the biggest limitations of HTA development. While hard to get around, a large class of applications doesn't need
Code:
Sleep
anyway.

And look how clean it is! No ugly
Code:
obj.WriteLn
s and quotes to clutter up your code.

The bulkiest thing here is the <hta:application /> tag, and the rest is plain sailing.

You start an HTA by double-clicking it's file icon or a shortcut to the file. From a cmd.exe prompt, you just enter the name as in sample.hta

I like to use Visual InterDev to work on HTAs because of the color-coding and code-completion it offers, along with a useful &quot;preview&quot; mode.
 
PHV, Dilettante,

Thanks for your help. I really liked the HTA, had never heard of them but they do exactly what I need. Am off to find out more about them.

Thanks again.

T
 
Dilettante,
let me starize you for this excellent post (as usual).
 
Gosh, thanks PHV.

I'm just a huge fan of the HTA. It could be one of the best RAD development platforms on Windows if a very few things were added, changed, or supported by an IDE targeted at them. Then if you had the option of compiling the script blocks into native code? Wow.

The biggest problem is really the HTA's exploitability by unscruplous people. That has led many of those in the &quot;protection racket&quot; (anti-virus tool providers, security advisors) to disable HTAs on many systems.

The good news (well, down the road a piece) is that Microsoft has not totally abandoned the idea. If you look closely at what will be coming in Windows Longhorn, in particular the new Avalon &quot;presentation subsystem,&quot; you will see that this is true.

In Avalon it appears that the existing DHTML and HTA concepts are being applied to .Net, albeit using a new markup technology called XAML. Besides using XAML instead of HTML and C# or VB.Net instead of JScript or VBScript it is almost exactly the same idea.

The big diference is that this is compiled managed code instead of wide-open script. That gives a developer (and users) a way to address security issues - without throwing the baby out with the bathwater. A couple releases down the road (next release after Whidbey?) Visual Studio ought to offer some really nice IDE support for Avalon too.

Here's one link:

 
Hello all,

I too like what dilettante contributed here. A nice demonstration of the hta tag.

Nonetheless, I think tfcrip's original posting deserves a straight answer along with the poster's argument. Else, it risks to give a wrong impression that externally driven internetexplorer.application's page cannot comply with event driven behavior. In fact, there are much more sophisticated scripting to render the page. But, the point is that as straightforward as a reasoning as tfcrip's or anybody else including myself is still a valid one. Here are some points I am obliged to make it good.

[1] A couple of typos like the ending </body></html> writeln etc.
[2] It is not necessary, in fact there is no reason to do so, to make two createobject(&quot;internetexplorer.application&quot;). It might again be a version not sufficiently cleaned up.
[3] As pointed out wscript quit naturally already if we do not make it alive with the instance life-cycle. Hence the do while true loop.
[4] The single most crucial point I have to make is that you must close the document for writing after opened it. Without this, event handling would fail utterly.

Hence, the script I revised would look like this.
Code:
Set objExplorer = wscript.CreateObject(&quot;InternetExplorer.Application&quot;,&quot;IE_&quot;)
objExplorer.Navigate &quot;about:blank&quot;  
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 800
objExplorer.Height = 570
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1

Set objDocument = objExplorer.Document    
objDocument.Open  

objDocument.Writeln &quot;<html><head><title>CCN Announcement</title>&quot;
objDocument.Writeln &quot;<script language=VBScript>&quot;
objDocument.Writeln &quot;Sub cmdOK_onClick()&quot;
objDocument.writeln &quot;msgbox &quot;&quot;onclick triggered.&quot;&quot;&quot;	'inserted for visual testing
objDocument.writeln &quot;WScript.Quit&quot;
objDocument.writeln &quot;End Sub&quot;
objDocument.writeln &quot;</script>&quot;
objDocument.writeln &quot;</head>&quot;
objDocument.Writeln &quot;<body bgcolor=#5959ab>&quot;
objDocument.Writeln &quot;<h2>Communication Details</h2>&quot;
objDocument.Writeln &quot;<Br>&quot;
objDocument.Writeln &quot;<Br>&quot;
objDocument.Writeln &quot;<input align=centre id=cmdOK type=Button name=cmdOK value=OK>&quot;
objDocument.Writeln &quot;</body></html>&quot;

'this is the single _most_ crucial step to make the script successful
objDocument.close

do while true : wscript.sleep 100 : loop

Sub IE_onQuit()
	msgbox &quot;onQuit event fired.&quot;
   Wscript.Quit
End Sub
regards - tsuji
 
Good points tsuji.

It is also important to avoid trying to manipulate the document before it has fully loaded too.

In addition, it is quite possible to &quot;wire up&quot; DHTML events from the document (once it has loaded) to handlers in the WSH script. Here is yet another take on the original example, written as a .WSF WSH script to demonstrate some other options:

sample.wsf
Code:
<?XML version=&quot;1.0&quot; standalone=&quot;yes&quot; ?>
<job>
  <resource id=&quot;resHTML&quot;>
    <![CDATA[
      <html>
        <head>
          <title>CCN Announcement</title>
        </head>
        <body bgcolor=#5959ab>
          <h2>Communication Details</h2>
          <br>
          <br>
          <input id=cmdOK type=Button value=OK>
        </body>
      </html>
    ]]>
  </resource>
  <script language=&quot;vbscript&quot;>
    <![CDATA[
      Option Explicit
      Dim objIE, objDoc
      Const READYSTATE_COMPLETE = 4
      Dim blnQuit

      Sub IE_OnQuit()
        MsgBox &quot;OnQuit event fired&quot;
        blnQuit = True
      End Sub

      Sub cmdOK_onclick()
        MsgBox &quot;onclick event fired&quot;
      End Sub

      Set objIE = WScript.CreateObject(&quot;InternetExplorer.Application&quot;, &quot;IE_&quot;)
      With objIE
        .Navigate &quot;about:blank&quot;
        .ToolBar = 0
        .StatusBar = 0
        .Width = 800
        .Height = 570
        .Left = 0
        .Top = 0
        Set objDoc = .document
      End With
      With objDoc
        .Open
        .Write GetResource(&quot;resHTML&quot;)
        .Close
        While objIE.ReadyState <> READYSTATE_COMPLETE
          WScript.Sleep 100
        Wend
        .all.cmdOK.onclick = GetRef(&quot;cmdOK_onclick&quot;)
      End With

      objIE.Visible = 1
      blnQuit = False
      Do
        WScript.Sleep 100
      Loop Until blnQuit
      Set objDoc = Nothing
      Set objIE = Nothing
    ]]>
  </script>
</job>
I've tried to be careful about disposing of object references here. I don't think current versions of WSH cause memory leaks if you fail to do this. It is considered &quot;good form&quot; and getting into the habit is strongly recommended, along with the use of Option Explicit in VBScript.
 
And while we're beating this dead horse... ;-)

Replace:
Code:
  </resource>
  <script language=&quot;vbscript&quot;>
    <![CDATA[
      Option Explicit
      Dim objIE, objDoc
Code:
      Const READYSTATE_COMPLETE = 4
Code:
      Dim blnQuit
by...
Code:
  </resource>
Code:
  <reference object=&quot;InternetExplorer.Application&quot; />
Code:
  <script language=&quot;vbscript&quot;>
    <![CDATA[
      Option Explicit
      Dim objIE, objDoc
      Dim blnQuit
This way you don't need to define the enum values exported by Internet Explorer. The <reference /> tag will load the type library's definitions, making them available to your code. This works for many objects including the FSO, saving you the grief of &quot;magic numbers&quot; in your code or a lot of Const definitions.

.WSF files are the 21st Century (starting with WSH 2.0) way of writing scripts, and offer many advantages over .VBS and .JS files.
 
Just as an addendum to this thread, can I suggest:
this site has some examples on using ie and vbs, but also has some ocx's that allow you to create modal and non-modal forms in vbs.

hth

Ben

----------------------------------------------
Ben O'Hara &quot;Where are all the stupid people from...
...And how'd they get so dumb?&quot;
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top