0

I've been experimenting with creating controls like buttons and textboxes on a userform in excel. That worked great, I even managed to catch events using minimalist event handler class.

Now I would like to create a userform object dynamically to house these controls and I would like to do it from vbscript rather than have to depend on excel for vba.

Can this be done ?

I found this VBA code that claims to create userform objects in excel/vba but it uses some excel only objects like "activeworkbook". Creating form programmatically in the module using vba

Community
  • 1
  • 1
Shodan
  • 1,065
  • 2
  • 13
  • 35
  • Possible duplicate of [Adding a GUI to VBScript](http://stackoverflow.com/questions/16052534/adding-a-gui-to-vbscript) – cyboashu Aug 16 '16 at 05:07

2 Answers2

0

In VBScript we don't have a forms package so we have to use other objects.

Html files can contain VBScript. If you rename a web page to *.hta it acts like a program.

This searches a hard drive. It has several html controls.

<HTML> 
<HEAD><TITLE>Simple Validation</TITLE> 
<SCRIPT LANGUAGE="VBScript">
Dim Dirname
Dim Searchterm
Dim FSO
Dim objOutFile


Sub Browse
    On Error Resume Next
    Set bffShell = CreateObject("Shell.Application")
    Set bff = bffShell.BrowseForFolder(0, "Select the My Documents folder", 9)
    If Err.number<>0 Then
        MsgBox "Error Setting up Browse for Folder"
    Else
        A = bff.ParentFolder.ParseName(bff.Title).Path
        If err.number=424 then err.clear
                tb2.value = A
    End If
End Sub

Sub Search
'    On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Searchterm", tb1.value
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Directory", tb2.value
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objOutFile = fso.CreateTextFile("results.txt",True)
    Dirname = tb2.value
    Searchterm = tb1.value
    ProcessFolder DirName
End Sub

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
        Set contents = thing.OpenAsTextStream
        If err.number = 0 then
        Linenum = 0
        Do Until contents.AtEndOfStream
            line = contents.readline
            Linenum = Linenum + 1
                Test = Instr(line, searchterm) 
                If Isnull(test) = false then If Test > 0 then ObjOutFile.WriteLine LineNum & "  " & thing.path
        Loop
        Else
            err.clear
        End If
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub

Sub Init
    On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    tb1.value = WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm")
    tb2.value = WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Directory")
End Sub

</script>
</head>
<body Onload=Init>
<p><INPUT Name=tb1 TYPE=Text Value="Search">
<p><INPUT Name=tb2 TYPE=Text Value="Folder"> <INPUT NAME="Browse" TYPE="BUTTON" VALUE="Browse" OnClick=Browse>
<p><INPUT NAME="Search" TYPE="BUTTON" VALUE="Search" OnClick=Search>
</body>
</html>

See https://msdn.microsoft.com/en-us/library/ms536495.aspx for properties such as setting an icon for the app.

EG

 <HTA:APPLICATION ID="oHTA"
     APPLICATIONNAME="myApp"
     BORDER="thin"
     BORDERSTYLE="normal"
     CAPTION="yes"
     ICON=""
     MAXIMIZEBUTTON="yes"
     MINIMIZEBUTTON="yes"
     SHOWINTASKBAR="no"
     SINGLEINSTANCE="no"
     SYSMENU="yes"
     VERSION="1.0"
     WINDOWSTATE="maximize"/>
Noodles
  • 11
  • 1
  • Thanks, that might have to do but I'm surprised the userform objects from vb or vba are not accessible at all ? Using HTA seems to sandbox the application quite a bit. I guess it does the job for a rudimentary interface. – Shodan Aug 16 '16 at 05:44
  • It was once the way of the future. You can insert the web browser control anywhere you can insert controls (Excel, VBA) and set a web page to it. – Noodles Aug 16 '16 at 11:22
0

Recently I found VbsForm here: http://maadam.sweb.cz/envbsform.htm. It can be used to create userform objects dynamically. A definition of form controls is done by comments. Here is a test code that I found there. Save it as form.vbs. Change the path to your vbsform.exe in the code (2x). Run your form.vbs code, not VbsForm.

' VB Script Document
option Explicit

'form..definition.begin
'form..delimiter|
'form..dont.close
dim fld'form..field|Input string:|200
dim ofd'form..openfile|Select file:|-1
dim fbd'form..opendir|Select dir:|-1
'form..label|This is a form..label test.  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
dim rbt'form..radiobutton||{Radio1|Radio2}
dim chk'form..checkbox|Checkbox:
dim dte'form..date|Date:
dim tme'form..time|Time:
dim dtm'form..datetime|Datetime:
dim cbo'form..combobox|Combobox:|0|{citem1|citem2|citem3|citem4}
dim lst'form..listbox|Listbox:|0|0|{litem1|litem2|litem3|litem4}
dim chl'form..chklistbox|Checklistbox:|-1|0|{form..field|form..openfile|form..opendir|form..radiobutton|form..checkbox|form..date|form..time|form..datetime|form..combobox|form..listbox|form..chklistbox|form..numericupdown}
dim num'form..numericupdown|Number:|1|5
dim sfd'form..savefile|Save file:|-1
'form..definition.end

if WScript.Arguments.Count = 0 then
  form_start ""
else
  if Wscript.Arguments(0) = "vbsform_exe" then
    fld = Wscript.Arguments(1)
    ofd = Wscript.Arguments(2)
    fbd = Wscript.Arguments(3)
    rbt = Wscript.Arguments(4)
    chk = Wscript.Arguments(5)
    dte = Wscript.Arguments(6)
    tme = Wscript.Arguments(7)
    dtm = Wscript.Arguments(8)
    cbo = Wscript.Arguments(9)
    lst = Wscript.Arguments(10)
    chl = Wscript.Arguments(11)
    num = Wscript.Arguments(12)
    sfd = Wscript.Arguments(13)
  else
    wscript.quit
  end if
end if

MsgBox "RETURN:  ~fld:" & fld & "  ~ofd:" & ofd & "  ~fbd:" & fbd & "  ~rbt:" & rbt & "  ~chk:" & chk & "  ~dte:" & dte & "  ~tme:" & tme & "  ~dtm:" & dtm & "  ~cbo:" & cbo & "  ~lst:" & lst & "  ~chl:" & chl & "  ~num:" & num & "  ~sfd:" & sfd,,"VBscript from VbsForm"

MsgBox "This is a VBScript MsgBox.  You will see another MsgBox after you click OK."

form_msgbox "This is VbsForm MsgBox with a countdown timer set to 10 seconds." & vbNewLine & "VbsForm MsgBox can show either a text or a picture." & vbNewLine & vbNewLine & "Bye.  5 4 3 2 1...", 10

wscript.quit




'______________________________________________________________________________

function form_start(idefnr)
  dim objshll
  Set objshll = CreateObject("WScript.Shell")
  objshll.Run "DRIVE:\path\to\vbsform.exe """ & WScript.ScriptFullName & trim(""" " & idefnr)
  wscript.quit
end function

'______________________________________________________________________________

function form_msgbox(itext, itimer)
  dim objshll
  Set objshll = CreateObject("WScript.Shell")
  objshll.Run "DRIVE:\path\to\vbsform.exe """ & itext & """ " & itimer
end function