Solved

Adding VBScripting

Posted on 2004-09-05
7
277 Views
Last Modified: 2010-04-17
Is it possible to have the program read from a text file that looks like this:


Sub EventJoin ()
<Code user wants>
End sub


Sub EventLeave ()
<Code User Wants>
End Sub


Reading it and then triggering the events?


0
Comment
Question by:andyakira
7 Comments
 
LVL 4

Accepted Solution

by:
divt earned 125 total points
ID: 11985331
Hi,

As far as I know, you can not 'import' the code from text file and use it as sub.
May I suggest you trythis:

1. let the main script take care of event trapping
2. seperate commands to execute for events in different files
3. load them up when event triggered
4. execute them accordingly

See the illustration below, which is tested.  Make sure you save these files in ascii format,
otherwise when you read the file back, you won't be able to execute them.
Good luck!



'---- main script begins ----
' This is in your main script
Sub EventJoin ()

   Dim fso, f1, ts, CommandList, f

   f="eventjoin.vbs"

   Const ForReading = 1
   Set fso = CreateObject("Scripting.FileSystemObject")
   ' Read the contents of the file.
   Set ts = fso.OpenTextFile(f, ForReading)
   CommandList = ts.ReadAll
   ts.Close
  Execute CommandList
End sub


Sub EventLeave ()
   Dim fso, f1, ts, CommandList, f

   f="eventleave.vbs"

   Const ForReading = 1
   Set fso = CreateObject("Scripting.FileSystemObject")
   ' Read the contents of the file.
   Set ts = fso.OpenTextFile(f, ForReading)
   CommandList = ts.ReadAll
   ts.Close
  Execute CommandList
End Sub
'---- main script ends ----


'---- eventjoin.vbs begins ----
' your list of commands here
msgbox "I run on EventJoin"
'---- eventjoin.vbs ends ----


'---- eventleave.vbs begins ----
' your list of commands here
msgbox "I run on EventLeave"
'---- eventleave.vbs ends ----
0
 
LVL 7

Assisted Solution

by:jimwasson
jimwasson earned 125 total points
ID: 11986252
You might want to consider using ExecuteGlobal which is poorly documented. A good explanation including examples of what you want toi do is included here:

http://www.winnetmag.com/WindowsScripting/Article/ArticleID/8979/8979.html
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 125 total points
ID: 11986494
I'm assuming VB6.
To do this you should use the "Microsoft Script Control 1.0"
Just add a reference to it in Project/References.
For an example see here:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21109601.html
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 125 total points
ID: 11987090
Well I think you can even do what you want, that is read from say a text file the contents of a procedure and execute the code.

What you can do is  Open two files one in Read Mode this will be the text file from which you want to read the contents and the other in Write Mode, say Dynamic.vbs read the contents of the procedure from your text file and write it into your new file i.e. Dynamic.vbs and then use the Shell object to run this new file.

I am assuming all the way that you are using VbScript, the same can however be done in VB also.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question