Question

I just started learning HTA and VBScript on my own a few weeks ago by starting this program for work. I'm still a novice so forgive me if this is a dumb question. I've been searching for two hours and either I'm incompetent or I have no idea how to search for the question.

I need this script to check for three files; if all three exist, the script will continue, if one is missing then a dialogue box will pop up telling the user which one is missing. I have looked at what feels like fifty different ways of doing this but none of them work for me. The following method is the only one that does not return any errors, the problem is it doesn't do anything at all, it's just a blank window. (I'm testing this by only checking for one file first)

    <SCRIPT LANGUAGE="VBScript">

    ' Resize and center the window
    ' ==========================================================
        sub DoResize
            window.resizeTo 350,250
                screenWidth = Document.ParentWindow.Screen.AvailWidth
                screenHeight = Document.ParentWindow.Screen.AvailHeight
                    posLeft = (screenWidth - 350) / 2
                    posTop = (screenHeight - 250) / 2    
            window.moveTo posLeft, posTop
        end Sub

        DoResize()

</SCRIPT>

<TITLE>Test</TITLE> 
    <HTA:APPLICATION  
        Id="oInstall"  
        APPLICATIONNAME="Test" 
        SCROLL="no" 
        SINGLEINSTANCE="yes" 
        WINDOWSTATE="normal" 
        SELECTION="NO" 
        CONTEXTMENU = "NO" 
        BORDER="Dialogue" 
        BORDERStyle = "Normal" 
        INNERBORDER = "YES" 
        NOWRAP 
        SYSMENU = "YES" 
    > 

<HEAD> 
    <STYLE type=text/css>
    textarea {
        overflow: hidden;
        color: #ffffff;
        border: none;
        background-color: transparent;
    }
    </STYLE>
    <body background="c:\mount\windows\system32\aopentools\Images\MSCBG.bmp">
</HEAD>
<textarea name="ProgSect" rows=1 cols=16 readonly></textarea><img src="c:\mount\windows\system32\aopentools\images\mscind.gif">
<SCRIPT LANGUAGE="VBScript"> 

         Option Explicit

Sub Check

    ' Creating objects
    ' ==========================================================

        Dim WshShell
        Dim objFSO, outFile
        Dim filesys

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set filesys = CreateObject("Scripting.FileSystemObject")
        Set oFSO=CreateObject("Scripting.FileSystemObject")

    ' ==========================================================

    If oFile=oFSO.FileExists("c:\users\jgainey\desktop\test.txt") Then

        BeginImage

    Else

        MsgBox "An Error Has Occurred" & vbNewLine & "EC03: Test.txt" & vbExclamation & "ERROR"
        Window.Close

    End If

End Sub

Sub BeginImage

    ' Creating objects
    ' ==========================================================

        Dim intWindowStyle
        Dim bWaitOnReturn
        Dim objShell
        Dim WshShell
        Dim objFSO, outFile
        Dim filesys

        Set objShell = CreateObject("Wscript.Shell")
        Set WshShell = CreateObject("WScript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set filesys = CreateObject("Scripting.FileSystemObject")
        set oFSO=CreateObject("Scripting.FileSystemObject")

            set oFile=oFSO.OpenTextFile("c:\users\jgainey\desktop\test.txt",1)
                text=oFile.ReadAll
                document.all.ProgSect.value=text

End Sub

        </SCRIPT> 
    </BODY> 
</HTML>

The result is a window with my background image, my .gif, my text area and that's it, the text area never populates. When I remove the test.txt file no message box pops up, nothing is impacted whatsoever.

Was it helpful?

Solution

You forgot to set up the event so that the Sub Check runs on page load, like so:

<body background="c:\mount\windows\system32\aopentools\Images\MSCBG.bmp" onload="Check">

Edit

Error free implementation of the Sub Check method:

Sub Check

    ' Creating objects
    ' ==========================================================

        Dim oFSO
        Set oFSO = CreateObject("Scripting.FileSystemObject")

    ' ==========================================================

    If oFSO.FileExists("c:\users\jgainey\desktop\test.txt") Then

        BeginImage

    Else

        MsgBox "An Error Has Occurred" & vbNewLine & "EC03: Test.txt" & vbExclamation & "ERROR"
        Window.Close

    End If

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top