0

I have programmed some Excel VBA and now I'm testing VBScript. I'm trying to create a form/window and after a while I managed to create this code:

Dim ExApp
ExApp = CreateObject("Excel.Application")
'ExApp.Application.Visible = True
Dim Workbook
Set Workbook = ExApp.Workbooks.Add
Dim myForm
Set myForm = Workbook.VBProject.VBComponents.Add(3)
Workbook.VBProject.VBComponents.Add(1).CodeModule.AddFromString "Function getForm()" & vbNewLine & "Set getForm = " & myForm.Name & vbNewLine & "End Function"
Dim userForm
Set userForm = ExApp.Run("getForm")
'VBA.UserForms.Add(myForm.Name).Show

It currently works but the method of creating a sub and calling/running it doesn't seem the best.

If I could call VBA.UserForms/UserForms then it would be better with the line VBA.UserForms.Add(myForm.Name).Show (works in VBA) but I can't figure out how since VBA is not an object in VBS or something like that.

I really can't find much information on this.

user7393973
  • 2,270
  • 1
  • 20
  • 58

1 Answers1

0

Since you're creating an Excel App object, I'm assuming you mean to create a form using a .vbs file outside of Excel. VBScript limits the amount of GUI you can use unless you go deep into COM objects - an example of which is what omegastripes is linking to in the comment above. But omegastripes does first mention 'HTA', which I've found is a pretty simple alternative (if you're already familiar with HTML). Here's an example taken from https://technet.microsoft.com/en-us/library/ee692768.aspx

<head>
     <title>Operating System Version</title>
     <HTA:APPLICATION 
          APPLICATIONNAME="Operating System Version"
          SCROLL="yes"
          SINGLEINSTANCE="yes"
     >
</head>

<script language="VBScript">

    Sub GetOSVersion
       strComputer = "."
           Set objWMIService = GetObject("winmgmts:\\" & strComputer &  "\root\cimv2")

           Set colOperatingSystems = objWMIService.ExecQuery _
               ("Select * from Win32_OperatingSystem")

           For Each objOperatingSystem in colOperatingSystems
               Msgbox objOperatingSystem.Caption & " " & _
                   objOperatingSystem.Version
           Next
    End Sub
</script>

<body>
<input type="button" value="Operating System" name="run_button"  onClick="GetOSVersion">

</body>

Save that code as 'test.hta' and double click on it in File Explorer.

sartoris
  • 816
  • 1
  • 7
  • 21