|
-
Feb 2nd, 2003, 10:08 AM
#1
Thread Starter
Member
[RESOLVED] Importing and Running VB methods from a text file
Hi, i have a problem which may not be possible to achieve, but here it is.
I will write some VB code into a plain text file using Notepad. I then want my proper VB program to open the text file, get all the text from it, then run the VB code obtained in that text.
for example, I might have a plain text file called demo.txt which will contain the following data
VB Code:
Sub showMsg()
MsgBox("this is a message")
End Sub
Now, I want my VB program to open this text file, read the vb code in in, and then run it, thus popping up a message box telling me "this is a message".
Is this possible?
If not, here is another way I might be able to achieve the same result. Would it be possible to write the VB code as a module file instead and dynamically interperate it by my VB program. I don't want to have to link the program and the module together myself, I want the program to link to the module, run it, then unlink from it again.
This would achieve the same purpose, if possible. Are any of these 2 methods possible, and how would I go about coding this?
Thanks for all your help guys, it is very appreciated.
WATTO
[email protected]
http://www.watto.org
Last edited by friendsofwatto; Feb 2nd, 2003 at 07:06 PM.
-
Feb 2nd, 2003, 10:25 AM
#2
PowerPoster
what you suggest is not possible directly but if you just put your text in a module file and include that module in your project, then it seems you've got what you want.
-
Feb 2nd, 2003, 10:42 AM
#3
Thread Starter
Member
Well, it isn't that simple unfortunately. This is what my program will end up doing.
My program will allow a user to write VB code into a textbox. When they click a button, it will save the code into a file. They can then, at a later date, get the VBCode from that file and run it as if it was hard coded into the program.
So you see, the code and file will be created dynamically. What about this, if the user saves it as a *.bas module file, is there some way I can tell the program to include that module file temporarily or perminantly? I would be really disappointed if this was not possible.
As a note, I cannot do this any other way, the code MUST be written out to a file and then read in from the file, I don't want to be able to take any "shortcut" methods which may be possible by skipping the file creation process or whatever. It must Write then Read to a file.
Thanks
WATTO
[email protected]
http://www.watto.org
"There is no such thing as a coincidence" - Tegan Sneddon 2002
-
Feb 2nd, 2003, 11:25 AM
#4
Frenzied Member
I was looking to do this myself a few months back. In the end I resigned myself to the fact that I would need to write my own interpreter and the customer would in effect run my predetermined code routines using a macro language that I would design and support.
If it is to allow a customer to run sequences in your code this will be good enough. Can you part with mor info on your applcationand, then maybe I can give you a more relevent example of what I mean.
-
Feb 2nd, 2003, 04:36 PM
#5
Thread Starter
Member
Thanks for that, i understand what you are saying. Unfortunately I don't think I would be able to pre-program all the possible code since I wanted them to use every VB command which is available using the default components - which means I would need to make presets for each piece of VB code 
Thanks for trying though, I am looking at other ways I may be able to do similar things, such as writing a C++ DLL to do the task (if this is possible) or by implimenting something that would allow the user to use another language like JavaScript to write the code, save it, and open in a VB web window.
Any suggestions would be appreciated. Thanks.
WATTO
[email protected]
http://wwww.watto.org
"There is no such thing as a coincidence" - Tegan Sneddon 2002
-
Feb 2nd, 2003, 04:42 PM
#6
Thread Starter
Member
What about this, ios this something which can be achieved (or atleast something to this effect)?
I list all the modules to include in the project inside a text file where each line is a new module to include. During the loading of the program, can I open this text file and include all the modules listed?
WATTO
[email protected]
http://www.watto.org
"There is no such thing as a coincidence" - Tegan Sneddon 2002
-
Feb 2nd, 2003, 06:02 PM
#7
Frenzied Member
Try checking out the "Microsoft Script Control". You'll have to add it as a reference in your project.
-
Feb 2nd, 2003, 06:17 PM
#8
Hyperactive Member
nope... i also tried this. Would be wonderful if it was possible, but it currently would require making your own compiler. Maybe if m$ put in a vb compiler into all versions of windows, it'd work, but they don't.
-
Feb 2nd, 2003, 06:23 PM
#9
Frenzied Member
Well I was sure that it was the same thing that VBS files use to run.
-
Feb 2nd, 2003, 07:02 PM
#10
Thread Starter
Member
THANK YOU SOOOO MUCH SHAWN!
I took a look at the Windows Script Control like you suggested and after alittle experimentation I finally got it working! Thanks heaps, you don't know how much this means to me!
To everyone else who wants to know how to do it, here you are. My project was to write VBscript into a text file, then have a VB program open the text file and run the code.
This is what I wrote in my text file, which I called "test.txt"...
VB Code:
Sub TestWindow()
MsgBox("HELLO")
End Sub
Now go to your program and click on the menu Project-->References and activate the component called Microsoft Script Control 1.0
Now here is the code for the program...
VB Code:
Sub testIt()
Open "test.txt" For Input As 1 'Input the file test.txt to the string CodeString
Do Until EOF(1)
Line Input #1, DataLine
CodeString = CodeString & Chr(13) & Chr(10) & DataLine
Loop
Close #1
Dim newSC As New ScriptControl 'Make a new ScriptControl
newSC.Language = "VBScript" ' Set the SC language to Visual Basic. Note that other languages such as JScript are also valid - check the help file associated with this command.
newSC.AddCode (CodeString) ' Add the code from the file to the SC
newSC.ExecuteStatement ("TestWindow") 'Run the code. Note it runs the statement "TestWindow" which is the name of the sub in the text file!
End Sub
Hope this helps out everyone who ever wanted to know how to do this 
Thanks everyone for your help!
WATTO
[email protected]
http://www.watto.org
"There is no such thing as a coincidence" - Tegan Sneddon 2002
-
Feb 2nd, 2003, 07:21 PM
#11
Frenzied Member
Glad I could help.
-
Feb 3rd, 2003, 12:36 AM
#12
Frenzied Member
Simply Superb Shawn - Thanks for figuring it out friendsofwatto
-
Feb 3rd, 2003, 04:23 AM
#13
Question: Isn't this just running script and not acutally proper VB code as originally requested?
-
Feb 3rd, 2003, 06:05 AM
#14
Member
yes it is not the same thing .. but I guess it depends on what he wants to do with it ... VB != VBS .. there are differences in the languages
-
Feb 3rd, 2003, 07:44 AM
#15
Thread Starter
Member
Oh ok, i didn't notice that it was running it as VBS rather than VB. For my purposes, VBS will be quite acceptable.
Glad I could help you out too David 
WATTO
[email protected]
http://www.watto.org
"There is no such thing as a coincidence" - Tegan Sneddon 2002
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|