Adding a GUI to VBScript

前端 未结 3 896
太阳男子
太阳男子 2020-12-03 23:39

I\'m currently working on a vbs script but I need user interaction with script. Basically I need two buttons and 4 checkboxes ( checkboxes isn\'t important).

Thanks

3条回答
  •  情深已故
    2020-12-03 23:56

    The best approach I found is to open HTA file from VBS using "WScript.Shell Run" and communicate back to the VBS using an XML file.

    Example (dialog.vbs)

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set oFile = fso.GetFile(Wscript.ScriptFullName) 
    sFolder = oFile.ParentFolder 
    sHtaFilePath = sFolder & "\dialog.hta"
    Dim oShell: Set oShell = CreateObject("WScript.Shell")
    oShell.Run sHtaFilePath, 1, True
    
    'Load return data from XML File
    If fso.FileExists(sHtaFilePath & ".xml") Then
        Set oXml = CreateObject("Microsoft.XMLDOM")
        oXML.async = False
        oXML.load sHtaFilePath & ".xml"
    
        MsgBox "Value 1: " & oXML.SelectSingleNode("/root/txt1").text
        MsgBox "Value 2: " & oXML.SelectSingleNode("/root/txt2").text
    
        fso.DeleteFile sHtaFilePath & ".xml"
    End If
    

    Example HTA (dialog.hta)

    
      Test
      
      
    
    
    
    
    
      
    Value 1
    Value 2

    Even better approach is for the HTA file to be created by the VBS file and then deleted. This way HTA does not have to be distributed along with the VBS.

    Set fso = CreateObject("Scripting.FileSystemObject") 
    
    sHtml = "
    Value 1 " & _ "" & _ "
    " & _ "
    Value 2 " & _ "" & _ "
    " & _ "

    " & _ " " & _ "" & _ "

    " Set oRet = OpenDialog(sHtml, "txt1,txt2", 300, 200, "Dialog 1") MsgBox "Value 1: " & oRet("txt1") & ", Value 2: " & oRet("txt2") '================================== Function OpenDialog(sHtml, sFields,iWidth,iHeight, sTitle) sHtaFilePath = Wscript.ScriptFullName & ".hta" CreateHtaFile sHtaFilePath, sHtml, sFields,iWidth,iHeight,sTitle Set f = fso.GetFile(sHtaFilePath) f.attributes = f.attributes + 2 'Hidden Dim oShell: Set oShell = CreateObject("WScript.Shell") oShell.Run """" & sHtaFilePath & """", 1, True If fso.FileExists(sHtaFilePath) Then fso.DeleteFile sHtaFilePath End If Set oRet = CreateObject("Scripting.Dictionary") 'Load return data from XML File If fso.FileExists(sHtaFilePath & ".xml") Then Set oXml = CreateObject("Microsoft.XMLDOM") oXML.async = False oXML.load sHtaFilePath & ".xml" For each sField In Split(sFields,",") oRet.Add trim(sField), oXML.SelectSingleNode("/root/" & trim(sField)).text Next fso.DeleteFile sHtaFilePath & ".xml" End If Set OpenDialog = oRet End Function Sub CreateHtaFile(sHtaFilePath, sHtml, sFields, iWidth, iHeight, sTitle) Set f = fso.CreateTextFile(sHtaFilePath, True) f.WriteLine "FL Reporting" f.WriteLine "" f.WriteLine "" f.WriteLine sHtml f.WriteLine "" f.Close End Sub

提交回复
热议问题