including classes with a wsc file - vbscript

Ok, what am i doing wrong here?
i'm trying to include a vbscript with a class inside this way:
SCRIPT.VBS:
set inc = createobject("script.runner")
inc.Include "class"
set x = new test
x.msg' here i get the error 'undefined class'!
REGISTERED .wsc file:
<?xml version="1.0"?>
<component>
<registration
description="wsc"
progid="script.runner"
version="1.00"
classid="{f65e154c-43b3-4f8f-aa3d-535af68f51d1}"
>
</registration>
<public>
<method name="Include">
<PARAMETER name="Script"/>
</method>
</public>
<script language="VBScript">
<![CDATA[
Sub Include(Script)
ExecuteGlobal(CreateObject("scripting.filesystemobject").OpenTextFile(Script & ".vbs", 1).Readall & VBNewLine)
End Sub
]]>
</script>
</component>
CLASS.VBS:
class test
public sub msg
msgbox "hi"
end sub
end class
I was thinking maybe i need to define it in the wsc file if i'm going to use classes or something?
i don't know..
Thanks for any help!

VBscript's Execute(Global) and .COM are very different ways of re-using code. You shouldn't mix them.
A .wsc lets you create one object and use its methods and properties. Such a method (factory) may create and return another object. So if you add
<method name="mkTest">
</method>
...
Function mkTest()
Set mkTest = New test
End Function
to your .wsc and
set x = inc.mkTest
x.msg
to your .vbs, the whole rigmarole will 'work'.
You should think about your real world task, read a good book about .COM, and come up with a simple strategy that does not mix heterogeneous technologies (maybe the Sub Include()/ExecuteGlobal approach sketched here).

did this:
script
set inc = createobject("script.runner")
inc.Include "C:\Users\GEEK\Desktop\small"
set x = inc.AddClass("test")
x.msg' here i get the error 'undefined class'!
inside wsc method
Function AddClass(ClassName)
execute("Set AddClass = New " & ClassName)
end Function
and Ekkehard.Horner,
You're right.
I'm just to curious about how to solve a problem even when there are easier ways to do
something ^^
Thanks for all the help!
regards

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.

SAP GUIXT - Pass Variable and run script

I would like to create an "input field" in SAP that uses the passed value within a vb script. To give a specific example, I would like to open FBL5N, pass an invoice into a field and view the invoice in VF03. The script to do that is ready and works for a hardcoded value of invoice or thru VBA.
Here is the GUI script part
inputfield (2,35) "inv" (2,45) size=10 name="V_inv"
pushbutton (toolbar) "print_inv" process="InvoiceScript.txt"
using MYINV = "V_inv"
Now, I don't know what to do for the inputscript part. I would like your assistance in this matter. Here is my first attempt:
Screen SAPLSLVC.0500
ApplyGuiScript template="VF03INV.vbs"
Enter
Thank you for your help and let me know if you need any precision.
Here are my sources of inspiration to get the above code :
http://www.synactive.com/tutor_e/lessonia03.html
http://www.synactive.com/docu_e/docia_process2.html
***if its possible to have a version that reads a value in clipboard, that would even be better.
After many attempts here is a solution:
Script(SAPLSLVC.0500.txt):
inputfield (2,35) "inv" (2,45) size=10 name="V_inv"
pushbutton (toolbar) "script" "/OVF03" process="startvf03.txt"
using INV = [V_inv] ' need this when opening new screen
InputScript (startvf03.txt):
parameter INV
Screen SAPMV60A.0101 'this is the VF03 screen
SET F[VBRK-VBELN] "&U[INV]" 'pass invoice # parameter
ApplyGuiScript "C:\guiXT\scripts\VF03INV.vbs"
VBScript (VF03INV.vbs):
inv = session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text
session.findById("wnd[0]/mbar/menu[0]/menu[11]").Select
session.findById("wnd[1]/tbar[0]/btn[37]").press
session.findById("wnd[0]/tbar[0]/okcd").Text = "pdf!"
session.findById("wnd[0]").sendVKey 0
'with some little extra here on how to save a pdf in SAP
'get new strings for locations (specific to my situation)
abc = session.findById("wnd[1]/usr/cntlHTML/shellcont/shell").Browserhandle.locationURL
beg = instr(abc,"C:")
cde = mid(abc,beg,9999)
dest = "C:\111\invoices\" & inv & ".pdf"
'changing from temp to a specific folder
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.copyfile cde, dest
'close the open file
session.findById("wnd[1]").close
session.findById("wnd[0]").close
Set fso = nothing
*the several If Not IsObject(application) Then you usually see were not necessary, but doesn't hurt to have them.
I hope this helps everyone learn Guixt
Within the VB script with the parameter "template" can you address a GuiXT variable as follows.
for example:
msgbox "&V[V_inv]"

Events not being raised by FileSystemWatcher in an integration test?

I'm trying to run an integration test on my class to make sure an event i expect to be raised is raised:
'integration test not unit test
<TestMethod()>
Public Sub Change_Network_File_Causes_Event_To_Be_Raised()
Dim EventCalled As Boolean
Dim deployChk = New TRSDeploymentCheck("foo")
deployChk._localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameLocalGUIDFile.txt")
AddHandler deployChk.DeploymentNeeded, Sub() EventCalled = True
deployChk.NetworkFileLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt")
ChangeNetworkFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt"))
Assert.IsTrue(EventCalled)
End Sub
Here is how i setup the FileSystemWatcher Object in my class:
Friend Property NetworkFileLocation As String
Set(value As String)
_netFileLoc = value
If File.Exists(value) Then
_watcher = New FileSystemWatcher(value.Replace(Path.GetFileName(value), String.Empty))
_watcher.EnableRaisingEvents = True
AddHandler _watcher.Changed, AddressOf OnNetworkFileChanged
End If
End Set
Get
Return _netFileLoc
End Get
End Property
Private Sub OnNetworkFileChanged(source As Object, e As FileSystemEventArgs)
If IsDeploymentNeeded() Then RaiseEvent DeploymentNeeded()
End Sub
I put a breakpoint in the OneNetworkFileChange sub. The breakpoint is never hit. I have verified the file is actually being changed in ChangeNetworkFile I even copied the code (except for hard coding the path) and copied it into a windows app which i ran during my unit test. It worked in my windows app. What am i missing here?
Finally figured it out after some testing. Well the reason EventCalled is never true above is because the "windows message pump" for the test is blocked. The event will be fired but only after the test completes (which of course is to late). So how do you fix it? Its kind of messy and i don't like it but i referenced System.Windows.Forms.dll & called Application.DoEvents()
'integration test not unit test
<TestMethod()>
Public Sub Change_Network_File_Causes_Event_To_Be_Raised()
Dim EventCalled As Boolean
Dim deployChk = New TRSDeploymentCheck("foo")
deployChk._localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameLocalGUIDFile.txt")
AddHandler deployChk.DeploymentNeeded, Sub() EventCalled = True
deployChk.NetworkFileLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt")
ChangeNetworkFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt"))
Application.DoEvents()
Assert.IsTrue(EventCalled)
End Sub
Until some tells me a better way this appears to be the solution.
Probably its the filter (string.Empty) which apparently only looks at files without an extension (that's an assumption).
Try "*.*" or something like this:
_watcher = New FileSystemWatcher(value.Replace(Path.GetFileName(value), string.Concat("*.", Path.GetExtension(value))))

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.

VB6 CallByName woes

I want to do a CallByName for some Subs but I just can't get it to go.
Everything goes fine until execution reaches the CallByName, then I have problems:
Whenever I use Me, it complains about a compile error
And if I use frmMyServer, it says "object or method not supported"
Question: How do I do this?
This is what I have :
in my 'modHandleData'
Private Sub HandleRequestScriptedNPC(...)
' ...
NPCMethod = "Scripted_Npc_" & NpcNum
CallByName Me, NPCMethod, VbMethod, NpcNum, Index
End Sub
in my 'modScriptedNPC'
Public Sub Scripted_Npc_9(ByVal NpcNum As Long, PlayerNum As Long)
SendOneOptionMsg PlayerNum, "NPC 9", "NPC 9 talks." & vbCrLf & "Then gives you a clue"
End Sub
You’re calling the code in a module, so there is no Me instance (that only exists in classes, including forms). My VB6 is a bit rusty, but I believe you cannot call methods in modules using CallByName since you need an object.

Resources