Results 1 to 8 of 8

Thread: text into sub?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    Hi everyone,

    this is a bit of a strange question and I don't think you can do it, but I thought I'd ask anyway.

    say you have a text file which contains vb code, is there anyway to say import that text file into your program to say turn it into a sub?

    for example say you had a sub called lets say crypto

    sub crypto

    end sub

    and that was all we had for the particular sub, now the text file we have, has the following code in it lets say:

    dim crypt as string
    dim cryp as string

    cryp = text2.text
    crypt = instr(1,cryp,"blah",vbTextCompare)

    if crypt <>0 then

    'more stuff here etc

    end sub

    now is there anyway to make the sub crypto imports that information???

    thanx for any help.

  2. #2
    Hyperactive Member Zaphod64831's Avatar
    Join Date
    Mar 2000
    Posts
    268
    Why not just copy and paste?
    Email: [email protected]

    Home Page: www.olemac.net/~hutch

    I'm bored, VERY bored, and I got bored with my sig. So I changed it to this.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    292
    I'm not sure but I think he means during runtime.
    "People who think they know everything are a great annoyance to those of us who do."

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    no, I think you can do it in Java though.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215

    Yeah I mean at run time.

    Yeah I mean at run time, I didn't think you could, it was just that my program gets data from a webpage, and if the webpages changes certain things in their layout and things then the way I get the data is invalid, and I have to make a new build sort of thing, I was just thinking instead of that maybe I could upload a text file to my site containg the latest code for extracting the code, and then download the text with the inet control, and import it into that sub, instead of having to keep making new builds sort of thing.

    thanx for replying.

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482

    Wink

    There are compiler issues trying to do what you've asked in VB alone, but a VB-VBA approach may work. Make sure your References include "Microsoft Visual Basic for Applications Extensibility"

    For demo sake, say you have textfile named mystuff.txt in c:\tmp that has:
    Attribute VB_Name = "Module10"
    Public Function Gee()
    MsgBox "hello world"
    End Function

    Now in VB:

    Private Sub JamCodeByWayOfOmaha_VBA()
    Dim xlapp As Object 'Excel.Application
    Dim xlbook As Object 'Excel.Workbook
    Dim xlmodule As Object 'VBComponents
    Dim xlmodule2 As Object
    Dim strCode As String
    ' Start Excel
    Set xlapp = CreateObject("Excel.Application")
    xlapp.Visible = false ' value may have to be true, I'm winging this.
    ' Add new workbook
    Set xlbook = xlapp.Workbooks.Add
    ' Add a module
    Set xlmodule = xlbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
    ' Add text by line to a macro in module
    strCode = "sub MyMysteryMac()" & vbCr & _
    " msgbox ""Hmm."" " & vbCr & "end sub"
    xlmodule.CodeModule.AddFromString strCode
    ' or save some grief and import from textfile
    Set xlmodule2 = xlbook.VBProject.VBComponents
    xlmodule2.Import "c:\tmp\mystuff.txt"
    ' Run macros
    xlapp.Run "MyMysteryMacro"
    xlapp.Run "Gee"
    ' Pause to pat self on back and watch purists' heads explode...
    MsgBox "All done, click to continue...", vbMsgBoxSetForeground
    ' Release modules & clean up
    Set xlmodule = Nothing: Set xlmodule2 = Nothing
    xlbook.Saved = false
    xlapp.Quit
    Set xlbook = Nothing: Set xlapp = Nothing
    End Sub

    Hope this helps.

    [Edited by Mongo on 05-04-2000 at 08:18 PM]

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215

    Talking THANKS!!!!

    Thanx Mongo, I really didn't think that it was possible, I thought it was a bit stupid me even asking that question, great code.

    But is there anyway to say put the function into a module you already have in your .exe? or make a module at run time in the exe and put the code in that sort of thing so that you can run the function in the module along side your form so you can send things to the form?

    thanx again.

    [Edited by Crypt on 05-05-2000 at 06:15 AM]

  8. #8
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482

    Red face

    At runtime there's plenty of deadwood space in a VB exe where you can write characters, but no, to my pea-brain knowledge, you cannot add function/sub functionality via a pre-post-compile methodology. You could expand the example I gave to store values on a worksheet and then read them from you VB app for use on a form or whatever.

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