Solved

Adding VBScripting

Posted on 2004-09-05
7
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

738 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