COM event handler in VBScript - events

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.

Related

How to call BlockInput in VBS

I have found on this site code for VBS to block user input. To simplify, code is:
Sub StopKeyMouse()
Set Def_DLL = DLL.DefineDLL("USER32")
Def_Proc = Def_DLL.DefineProc("BlockInput", vt_b1, vt_b1)
Set Lib = DLL.Load("USER32.DLL", "USER32")
Lib.BlockInput(True)
End Sub
Sub ResumeKeyMouse()
Set Def_DLL = DLL.DefineDLL("USER32")
Def_Proc = Def_DLL.DefineProc("BlockInput", vt_b1, vt_b1)
Set Lib = DLL.Load("USER32.DLL", "USER32")
Lib.BlockInput(False)
End Sub
Sub Test()
StopKeyMouse()
WScript.Sleep 1000
ResumeKeyMouse()
End Sub
Test()
When I run it, I get the error Object required: 'DLL'. Since the post is from 2004, I assume that VBS interaction with User32.dll has been changed.
I am missing a line with CreateObject, something like Set DLL = CreateObject("User32.dll").
Does anybody know what is correct code for script to work?
I have also found that it was possible to use
Set oAutoIt = CreateObject("AutoItX.Control")
oAutoIt.BlockInput "on"
But this is obsolete.
Is it possible to call BlockInput from VBS?
Thank you for any help.

AutomationAnywhere 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

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.

How to add default signature and new tables in a new outlook mail by testcomplete

I am trying to send new outlook mail by testcomplete using vb scripting language .In that new mail i want to add new tables and keep default signature at bottom of mail by testcomplete. i am getting VB script run time error when i am using this code..please check the code and suggest me the correct methods i have to use to add new tables and signature
Function SendMail()
Dim objOutLook, NamespaceMAPI,objNewMail, fso, SendReceiveControls
Dim strTo,strCc ,strBcc ,strSubject, AccountName,strAttachmentPath
strSubject="test"
strTo=yyy#yy.com
strCc=XXX#XX.com
strBcc =zzz#zzz.com
strAttachmentPath="c:\text.txt"
Set objOutLook = CreateObject("Outlook.Application")
Set NamespaceMAPI = objOutLook.GetNamespace("MAPI")
Set objNewMail = objOutLook.CreateItem(olMailItem)
objOutLook.DisplayAlerts =True
objNewMail.TO = strTo
objNewMail.CC = strCc
objNewMail.BCC=strBcc
objNewMail.Subject = strSubject
objNewMail.Body = strMsg
If strAttachmentPath <> "" Then
Set fso =CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strAttachmentPath) Then
objNewMail.Attachments.Add(strAttachmentPath)
objNewMail.GetDefaultsignature() 'script run time error occured here
objNewMail.addtable(4,3)
objNewMail.display
Else
msgbox "Attachment File Does not exists"
End If
End If
objOutLook.Quit
''''''' Releasing objects '''''''
Set objOutLook =Nothing
Set objNewMail = Nothing
Set fso = Nothing
End Function
please help me.. thanks in advannce....
See if this or this helps. They are alternative methods to yours.
I prefer to use the 2nd option, the CDO method, you just need to take atention to the fact that usually this email goes to the spam inbox, you need to manually add it to your secure contacts

VBScript Code: Object required error

Guys
I just wrote a vbs code snippet to automatically post in a forum. That's just for convenience. I encountered a wired problem:
I have several accounts. When I login as some accounts and use the script to automatically post, everything is OK. However, when I login as the other accounts and run the script, I got the error:
Error : Object required "getElementById(...)"
Code : 800a01a8
Source : Microsoft VBScript runtime error
I'm sure the object exists because I get it in the source of the webpage. I feel the error occurred randomly and I cannot get the regularity.
The script is ran in a Windows 8 OS and the browser is IE9. I'm a new learner of vbs and I don't know how to debug it. So I hope someone can help me. You can give me some clue.
Here is my code snippet:
Option Explicit
Dim IEApp
Dim iURL1
Dim iURL2
Dim iURL3
Dim iURL4
Dim iURL5
Set IEApp = CreateObject("InternetExplorer.Application")
iURL1="http://bbs.dealmoon.com/thread-299027-1-1.html"
iURL2="http://bbs.dealmoon.com/thread-299195-1-1.html"
iURL3="http://bbs.dealmoon.com/thread-299018-1-1.html"
iURL4="http://bbs.dealmoon.com/thread-299015-1-1.html"
iURL5="http://bbs.dealmoon.com/thread-299014-1-1.html"
Open iURL1
Open iURL2
Open iURL3
Open iURL4
Open iURL5
WScript.Echo("Done!")
Sub Wait(IE)
Do
WScript.Sleep 500
Loop While IE.ReadyState < 4 And IE.Busy
Do
WScript.Sleep 500
Loop While IE.ReadyState < 4 And IE.Busy
End Sub
Sub Post(IE)
Dim count
For count=0 To 9
With IE.Document
.getElementById("fastpostmessage").innerHTML = "good"
.getElementById("fastpostsubmit").click
Wait IE
WScript.Sleep GetRandom(7,15)
End With
Next
End Sub
Sub Open(PageURL)
IEApp.Visible = False
IEApp.Navigate PageURL
Wait IEApp
Post IEApp
End Sub
Function GetRandom(floor,ceil)
Randomize
GetRandom=Int((ceil - floor + 1) * Rnd + floor)*1000
End Function
You should call your POST routine after all the HTML content is loaded, for example in DOMContentLoaded or window.onload (for IE<9).
You dimmed IEApp, created the object, but you are not using it when trying to get the element ID. Your With block should look like this:
With IEApp.Document
.getElementById("fastpostmessage").innerHTML = "good"
.getElementById("fastpostsubmit").click
Wait IEApp
WScript.Sleep GetRandom(7,15)
End With
There are a few other places you only have IE instead of IEApp. Clear those up and your code should run fine.

Resources