Results 1 to 15 of 15

Thread: [RESOLVED] Importing and Running VB methods from a text file

  1. #1

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52

    [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:
    1. Sub showMsg()
    2.   MsgBox("this is a message")
    3. 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.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    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.

  3. #3

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52
    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

  4. #4
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    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.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  5. #5

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52
    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

  6. #6

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52
    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

  7. #7
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Try checking out the "Microsoft Script Control". You'll have to add it as a reference in your project.
    Please rate my post.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    352
    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.

  9. #9
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Well I was sure that it was the same thing that VBS files use to run.
    Please rate my post.

  10. #10

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52
    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:
    1. Sub TestWindow()
    2. MsgBox("HELLO")
    3. 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:
    1. Sub testIt()
    2.  
    3. Open "test.txt" For Input As 1 'Input the file test.txt to the string CodeString
    4. Do Until EOF(1)
    5.   Line Input #1, DataLine
    6.   CodeString = CodeString & Chr(13) & Chr(10) & DataLine
    7.   Loop
    8. Close #1
    9.  
    10. Dim newSC As New ScriptControl 'Make a new ScriptControl
    11. 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.
    12. newSC.AddCode (CodeString) ' Add the code from the file to the SC
    13. newSC.ExecuteStatement ("TestWindow") 'Run the code. Note it runs the statement "TestWindow" which is the name of the sub in the text file!
    14.  
    15. 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

  11. #11
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Glad I could help.
    Please rate my post.

  12. #12
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Simply Superb Shawn - Thanks for figuring it out friendsofwatto
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  13. #13
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    Question: Isn't this just running script and not acutally proper VB code as originally requested?

  14. #14
    Member
    Join Date
    Mar 2002
    Location
    Canada
    Posts
    51
    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
    ~T

  15. #15

    Thread Starter
    Member friendsofwatto's Avatar
    Join Date
    Jan 2003
    Location
    Aussie-land
    Posts
    52
    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
  •  



Click Here to Expand Forum to Full Width