AutomationAnywhere VBScript - vbscript

Hello I am trying to upload VBScript to filter out only data that has been changed. When I am using script as a Macro it works but when I am launching it via AAE it throws an
Error In Script 1024 Expected Statement.
enter image description here
Sub filtering()
Range("H3").AutoFilter Field:=8, Criteria1:="<>"
Range("Q3").AutoFilter Field:=17, Criteria1:="<>"
Range("P3").AutoFilter Field:=16, Criteria1:=">=" & Range("A1").Value
Operator:=xlAND Criteria2:="<=" & Range("A2").Value
End Sub

There are 2 approaches to solve this :
First:
Lets include the subroutine withing within the <script> tag
<script type="text/vbscript">
Sub filtering()
Range("H3").AutoFilter Field:=8, Criteria1:="<>"
Range("Q3").AutoFilter Field:=17, Criteria1:="<>"
Range("P3").AutoFilter Field:=16, Criteria1:=">=" & Range("A1").Value
Operator:=xlAND Criteria2:="<=" & Range("A2").Value
End Sub
</script>
Or ignore the Subroutines entirely and include the below snippet in the .vbs file in AAE
Range("H3").AutoFilter Field:=8, Criteria1:="<>"
Range("Q3").AutoFilter Field:=17, Criteria1:="<>"
Range("P3").AutoFilter Field:=16, Criteria1:=">=" & Range("A1").Value
Operator:=xlAND Criteria2:="<=" & Range("A2").Value
Second:
You can the add the script in as an Add-in button in the form of Ribbons
And in AAE you can instruct the button to click on the button by using the Object Cloning from the command library.
For example:
How the button looks like from Excel Ribbons
The Code module in the back end

Related

How do I write text box to .txt in VBScript?

i need to write a text area (box) value to .txt i am getting permissions denied when writing to "input.txt"
i got got permission denied when writing input.txt from the text box. i edited using suggestions found below and also killed handle for input.txt using process explorer.
using the code below i can now save textbox text to text file input.txt.
i also figured out how to call upon a batch file to change that text into a cyphered form, and then i added a refresh button to open that changed text into a second textbox as a final output result which leaves the text now capable of being password encrypted after being saved in input.txt and opened in a second textbox from file SR-Encrypted.txt after being cyphered via batch file encrypter.bat then by refreshing the page using the refresh button i added..
thank you agnar!
<html>
<script language="vbscript">
option explicit
Const ForWriting = 2
Dim objFSO, objFile, strFileName, objshell
strFileName = "input.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Sub Submitarea
Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
objfile.Write TextFile.Value
objFile.Close
MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub
</script>
</html>
</head>
<title>Example</title>
<script language="VBScript">
Sub test
set oFSO=CreateObject("Scripting.FileSystemObject")
set oFile=oFSO.OpenTextFile("SR-Encrypted.txt",1)
text=oFile.ReadAll
document.all.ScriptArea.value=text
Set objFile = nothing
oFile.Close
End Sub
</script>
</head>
<script language="vbscript">
Option Explicit
' This is the Sub that opens external files and reads in the contents.
' In this way, you can have separate files for data and libraries of functions
Sub Include(yourFile)
Dim oFSO, oFileBeingReadIn ' define Objects
Dim sFileContents ' define Strings
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFileBeingReadIn = oFSO.OpenTextFile("try.vbs", 1)
sFileContents = oFileBeingReadIn.ReadAll
oFileBeingReadIn.Close
ExecuteGlobal sFileContents
End Sub
' Here we call the Include Sub, then pass it the name of the file we want items from
Include "mySubLib"
</script>
<body>
<button onClick="test()">Refresh Message Encrypter-Decrypter</button>
</body>
</html>
</head>
</html>
<body>
<h1>Write File</h1>
<p>How to Write to a File.</p>
<textarea name="TextFile" id="TextFile" rows="20" cols="50"></textarea>
<input type="button" value="Submit" onclick="Submitarea">
</body>
<body>
</body>
</html>
The "permission denied" errors most likely occur because the file had already been opened. Open the file only when you actually want to write to it, and close it right away after you finished writing. Also, if you want to write the content of the text area to the file you need to actually write the content of the text area, not the string "Txtarea".
Change this:
Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
Set objShell = CreateObject("WScript.Shell")
Sub Submitarea
sTxtarea = TextFile.Value
objfile.Write "Txtarea" & vbCrLf
MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub
to this:
Set objShell = CreateObject("WScript.Shell")
Sub Submitarea
Set objFile = objFSO.OpenTextFile(strFileName, 2, True)
objfile.Write TextFile.Value
objFile.Close
MsgBox "Your text has been added to " & strFileName, 64, "Textarea Input"
End Sub
and the problem will disappear.
Change the second parameter of the OpenTextFile() method from 2 to 8 if you want to append to the output file rather than replace its content.
#ansgar wiechers edits and suggestions were all correct and resulted in the scripts functioning properly. see code above in question for accurate solution to the original question.
permissions denied were caused by an open handle on input.txt. using process tree i killed the handle and the file functions properly.
the suggestion posted by ansgar wiechers fixed the code which was another separate issue but the correct codes and information reguarding the original question are in the post. thank you.

vb script to trigger class onclick event with no ID or Value on web page

what Im trying to do is very simple. A GPO that runs a cloud-based install once. I just cannot find the correct VB syntax to simply click on a button on a webpage that starts an installation. I have a runonce batch file in a GPO that calls the vbs file which currently contains...
Sub test()
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "http://xxxxxxx.com"
Do While .Busy Or .readyState <> 4
WScript.Sleep 50
Loop
Set oInputs = .Document.getElementsByTagName("input")
For Each elm In oInput
If elm.Value = "the HTML has no valued or ID" Then
elm.Click
Exit For
End If
Next
Do While .Busy Or .readyState <> 4
WScript.Sleep 50
Loop
End With
End Sub
The HTML element i am trying to trigger is...
<a class="button" onclick="DownloadSubmit();" href="" target="_blank"></a>

VBScript / WshShell - Click Button with no name, id, or class

I want to use VBScript to click a button that reads “Add Document” in the GUI of an application that runs in Internet Explorer. The problem is there is no “id”, “name” , or “class” element in the source code (see below) to use as a reference in the script.
Source Code:
<TD>
<INPUT type="button" value="Add Document" onclick="addFileUploadBox()">
</TD>
My code is below and works for elements that have source code ids. Also included are some failed attempts to click the button in question based on my googling but I cant get to work. I'm new to this and any help would be appreciated - any suggestions to fix?
My Code:
Dim objIE
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "http://sharemxxxx.xxxxx.com/xxxImport/import.aspx"
While objIE.Busy
WScript.Sleep 100
Wend
objIE.Document.getElementById("txtReferenceNum").value = "15226002"
objIE.Document.getElementById("ddlCategory").value = "05"
objIE.Document.getElementById("btnRetrieve").click
' ABOVE CODE WORKS
' NOT WORKING #1 - DOES NOTHING - NO ERROR
'===========================================
objIE.Document.getElementByType("btn").Value="Add Document"
For Each btn In objIE.Document.getElementsByTagName("input")
If btn.Value="Add Document"Then btn.Click()
Next
' NOT WORKING #2 - DOES NOTHING - NO ERROR
'===========================================
For Each Button In objIE.Document.getElementsByTagName("input")
If Button.Value="Add Document"Then Button.Click()
Next
' NOT WORKING #3 - DOES NOTHING - NO ERROR
'===========================================
Set oInputs = objIE.Document.getElementsByTagName("input")
For Each elm In oInputs
If elm.Value = "Add Document" Then
elm.Click
Exit For
End If
Next
' NOT WORKING #4 - Show Empty Msg Box
'===========================================
set objButtons = objIE.document.getElementsByTagName("input")
for each objButton in objButtons
strText = objButton.innerhtml
msgbox strText
if (strText = "Add Document") then
msgbox "found the button!"
objButton.click
exit for
end if
next
You probably don't even need to find the element. It looks like the button just calls a JavaScript function. So you can use the execScript() function to run it.
For example:
objIE.Document.parentWindow.execScript "addFileUploadBox();", "javascript"

How to work with user's text entry in an HTA form

I have the a button on my hta file that when i click on will trigger a batch file to run. I want user to input the computer name into a text box and use it at the below psexec command which requires the computer name. Any recommendations would greatly appreciated.
<script language="VBScript">
Sub InstallVNC
dim shell
set shell=createobject("wscript.shell")
shell.run "psexec -u domain01\username -p password \\textbox1.value -c \\doamin\SHARE\SOFTWARE\install_program.bat"
End Sub
<body bgcolor="buttonface">
<p><font face="verdana" color="red">Application Installer</font></p>
Please run as administrator. <p>
<form name="test">
<font>Computer Name:</font>
<input type="text" name="textbox1" id="textbox1">
</form>
<input id=runbutton class="button" type="button" value="Install VNC" name="db_button" onClick="installvnc"><p>
</body>
</html>
You need to handle the input string differently. Something like this:
Sub InstallVNC
dim shell
dim strInput
dim shell_parameter
strInput = textbox1.value
shell_parameter = "psexec -u domain01\username -p password \\" & strInput & " -c \\doamin\SHARE\SOFTWARE\install_program.bat"
set shell=createobject("wscript.shell")
shell.run shell_parameter
End Sub
About getting the user input: I haven't used <form> in hta -- a dialog box has always been simpler. I wonder if that would get you past the problem. This code would be invoked when the button is pressed:
Dim strDialogPrompt, strDialogTitle, strDialogDefault
strDialogTitle = "File location"
strDialogPrompt = "Enter location of file to install." _
& vbCrLf & "(You must run this as Administrator.)"
strDialogDefault = "Z:\The_Usual_Path"
strInput = InputBox(strDialogPrompt, strDialogTitle, strDialogDefault, 150, 150)
Basic DOM manipulation:
Dim computerName
computerName = document.getElementById("textbox1").value
For more information, you can consult pretty much any DOM reference (e.g., https://developer.mozilla.org/en-US/docs/DOM)

COM event handler in VBScript

I want to catch the NewCivicAddressReport event which means I need to implement the event handler. Can anyone explain why the VBScript code embedded in html page works but the VBS file doesn't?
Here is the html page where NewCivicAddressReport events can be handled in the CivicFactory_NewCivicAddressReport() function. I suppose it is because of the event handler naming convention. Correct me if I'm wrong.
<!-- Civic address Location report factory object -->
<object id="CivicFactory"
classid="clsid:2A11F42C-3E81-4ad4-9CBE-45579D89671A"
type="application/x-oleobject">
</object>
<script language="vbscript">
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "New civic address report!"
End Function
Sub OnLoadPage()
CivicFactory.ListenForReports(1000)
End Sub
Sub DisplayStatus(status)
MsgBox "status displayed"
End Sub
</script>
And below is the VBS file which doesn't work - the event handler function seems never gets called.
Dim CivicFactory
Set CivicFactory = WScript.CreateObject("LocationDisp.CivicAddressReportFactory")
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "Location changed!"
keepSleeping=false
End Function
CivicFactory.ListenForReports(1000)
dim keepSleeping
keepSleeping=true
while keepSleeping
WScript.Sleep 200
wend
By the way, can anyone tell me the difference between the two ways of creating an object: and WScript.CreateObject()?
Thanks in advance!
The second argument for WScript.CreateObject is the prefix used in your event-handling Functions. For it to work, change your call to CreateObject to the following.
Set CivicFactory = _
WScript.CreateObject("LocationDisp.CivicAddressReportFactory", _
"CivicFactory_")
The difference between WScript.CreateObject and CreateObject is that WScript.CreateObject supports events.

Resources