Hello, I'm trying to basically achieve a program that would READ an EXTERNAL FILE (say "File.QPP"), which contains an actuall, VB6-style of a code (Example: 'msgbox "TEST" '), Upon opening/reading the file, my executable should EXECUTE the code INSIDE "File.QPP"... note: Execute as in basically run the given command, NOT TO COMPILE..
Is this even possible?
I would kindly appreciate .ZIP project files,
Many thanks..
DMZ.
the master
01-14-2008, 04:54 AM
You can use the script control to execute VB code at runtime
Also see Load a text file (http://www.devx.com/vb2themax/Tip/18271)
Thank you for replying, I'm not sure how can I use this code you have supplied, could you please explain yourself?
Thanks.
the master
01-14-2008, 05:46 AM
I would recommend using the second example on that site
private Function FileText(ByVal filename As String) As String
Dim handle As Integer
' ensure that the file exists
If Len(Dir$(filename)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
handle = FreeFile
Open filename$ For Binary As #handle
' read the string and close the file
FileText = Space$(LOF(handle))
Get #handle, , FileText
Close #handle
End Function
That function loads a text file and returns it. If you want to test it out then you can try this code
dim temp as string
temp = FileText("C:\test.txt")
msgbox temp
Obviously insert a filename that exists instead of "C:\test.txt". The file will be loaded into a variable called "temp" which is declared as a string. The above example will show the file in a message box so you can see that the FileText() function is working.
That handles the loading part of your app. Now you want to run the commands that were in the file so you will need to add the "Microsoft Script Control" from the components list. You can then use the script control to execute the code you just loaded from the text file
I would recommend using the second example on that site
private Function FileText(ByVal filename As String) As String
Dim handle As Integer
' ensure that the file exists
If Len(Dir$(filename)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
handle = FreeFile
Open filename$ For Binary As #handle
' read the string and close the file
FileText = Space$(LOF(handle))
Get #handle, , FileText
Close #handle
End Function
That function loads a text file and returns it. If you want to test it out then you can try this code
dim temp as string
temp = FileText("C:\test.txt")
msgbox temp
Obviously insert a filename that exists instead of "C:\test.txt". The file will be loaded into a variable called "temp" which is declared as a string. The above example will show the file in a message box so you can see that the FileText() function is working.
That handles the loading part of your app. Now you want to run the commands that were in the file so you will need to add the "Microsoft Script Control" from the components list. You can then use the script control to execute the code you just loaded from the text file
Thousand Thankyou's, it workes like a charm!
What other "Languages" are available in the ScriptControl1 "Language" property? Is there pure VB rather then VBScript?
Can the script ran use modules that were already added to the project?
Can you add BAS/Modules via the script control?
Thanks.
the master
01-14-2008, 06:31 AM
Im not sure about the names but when ive used it it did just about anything that normal VB can do. Aparently it supports javascript and possibly C++ too but i dont know for sure.
You can do all kinds of things in VB though. Its been a while since i used it but i think you add forms or objects to it and you can access them just like you were coding in the normal VB IDE
-- I'm sorry, but I really need to ask these questions, please forgive me, I'm just not experienced enough ...
Can the script ran use modules that were already added to the project?
Can you add BAS/Modules via the script control?
Thanks.
tHuNd3r
01-14-2008, 08:35 AM
No...
You can create some DLLs/Classes and Instantiate them via the Script...
but you can't access the bas/modules of your project from the script...
loquin
01-14-2008, 10:20 AM
Also, keep in mind that script is a subset of VB's commands. Many VB functions/subs are not available when using the scripting control, including File I/O, Registry I/O and Replace.