• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

How to make a vb script

Hi!

Have a prosject in vb6.0 that have a sub, that can be very different from
customer to customer.

Is it possible to use a vb script in vb6.0. That have this sub, so the customer can change
the code in this script.

New to vb scripts, so how to make the script. And how do i use it in vb6.0 ?

Please help ASAP.

Thanks

Tor
0
team2005
Asked:
team2005
  • 5
  • 3
  • 3
2 Solutions
 
fds_fatboyCommented:
Call it something like this (With a reference to msScript control in your project)...

    ...
    Set scrScript = New ScriptControl
    scrScript.Language = "VBScript"
    Call scrScript.AddCode(strMyScriptInAString)
    varScriptValue = scrScript.Run("YourSubName, varParams)
    Set scrScript = Nothing
    ...
0
 
team2005Author Commented:
Hi!

But this scriptfile, what is the extension name of this ?

How is the scriptfile lock like ?

If you can show me how the script look like if i want this sub. I vil be very happy.
Never made a vb script before:

Public Sub Prep(param1 as String, param2 as String)
   Dim lngInFile As Long
   Dim lngOutFile As Long
   Dim strIn As String
   Dim strOut As String
   Dim lngCounter As Long
   Dim intPos As Integer
   Dim strReplace As String
   Dim dooloop As Long
   Dim s1 As String
   
   lngInFile = FreeFile
   
   Open param1 For Input As lngInFile
   
   lngOutFile = FreeFile
   
   Open param2 For Output As lngOutFile
   
   While Not EOF(lngInFile)

       Line Input #lngInFile, strIn
       intPos = InStr(strIn, "}")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "å"
           'do something...
           strIn = Replace(strIn, "}", strReplace, , 1)
           intPos = InStr(strIn, "}")
       Wend
       
       intPos = InStr(strIn, "]")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "Å"
           'do something...
           strIn = Replace(strIn, "]", strReplace, , 1)
           intPos = InStr(strIn, "]")
       Wend
       
       intPos = InStr(strIn, "\")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "Ø"
           'do something...
           strIn = Replace(strIn, "\", strReplace, , 1)
           intPos = InStr(strIn, "\")
       Wend
       
       intPos = InStr(strIn, "|")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "ø"
           'do something...
           strIn = Replace(strIn, "|", strReplace, , 1)
           intPos = InStr(strIn, "|")
       Wend
       
       intPos = InStr(strIn, "{")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "æ"
           'do something...
           strIn = Replace(strIn, "{", strReplace, , 1)
           intPos = InStr(strIn, "{")
       Wend
       
       intPos = InStr(strIn, "[")
       While intPos > 0
           lngCounter = lngCounter + 1
           strReplace = "Æ"
           'do something...
           strIn = Replace(strIn, "[", strReplace, , 1)
           intPos = InStr(strIn, "[")
       Wend
       Print #lngOutFile, strIn
   Wend

   Close lngInFile
   Close lngOutFile

End Sub


Thanks you for helping me out here..

Cheers,
Tor
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vinnyd79Commented:
In vbscript you could try something like this. Just write it to a text file with a .vbs extension:

Dim lngInFile
Dim lngOutFile
Dim strIn
Dim strOut
Dim strReplace
Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")
Set lngInFile = fso.OpenTextFile("C:\InputFile.txt",1, True)
strIn = lngInFile.ReadAll
lngInFile.Close

strReplace = "å"
strIn = Replace(strIn, "}", strReplace)

strReplace = "Å"
'do something...
strIn = Replace(strIn, "]", strReplace)

strReplace = "Ø"
'do something...
strIn = Replace(strIn, "\", strReplace)


Set fso = CreateObject("Scripting.FileSystemObject")
Set lngOutFile = fso.CreateTextFile("C:\OutFile.txt",2, True)
lngOutFile.Write strIn
lngOutFile.Close

Set lngInFile = Nothing
Set lngOutFile = Nothing
Set fso = Nothing
0
 
vinnyd79Commented:
To run the script from vb6 you could use the shellexecute API:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Call ShellExecute(0, "Open", ("C:\MyFile.vbs"), "", "", 1)
End Sub
0
 
fds_fatboyCommented:

>> To run the script from vb6 you could use the shellexecute API:

Yes, but the scriptcontrol returns you exception messages.
0
 
fds_fatboyCommented:
Also, If you prefer to stick to VB executables/dlls, you could always implement a plugin interface as an alternative to custom scripts
One option is to create a dll based on a template with a standard (binary compatible) interface  per customer. Keep the name the same for each customer. Develop your executable against the template/test version. When you ship your executable, each user can have their customised code in their own version of the DLL. Your code will just call standard methods/properties through standard interfaces to apply the customers customisations.

A similar (but more complex) design is discussed here:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21157562.html
0
 
team2005Author Commented:
Hi!

Tryed your code, working 100%. Thanks...

But one thing, how do i use parameters in the script, and how do i
call the script from vb6. Using parameters ?

You are the best...

Thanks

Tor
0
 
vinnyd79Commented:
Not sure who you were referring to but you could use wscript.arguments to get the command line arguments:

Dim lngInFile
Dim lngOutFile
Dim strIn
Dim strReplace
Dim fso

Dim InFile,OutFile

InFile = wscript.arguments(0)
OutFile = wscript.arguments(1)

Set fso = CreateObject("Scripting.FileSystemObject")
Set lngInFile = fso.OpenTextFile(InFile,1, True)
strIn = lngInFile.ReadAll
lngInFile.Close

strReplace = "å"
strIn = Replace(strIn, "}", strReplace)

strReplace = "Å"
'do something...
strIn = Replace(strIn, "]", strReplace)

strReplace = "Ø"
'do something...
strIn = Replace(strIn, "\", strReplace)


Set fso = CreateObject("Scripting.FileSystemObject")
Set lngOutFile = fso.CreateTextFile(OutFile,2, True)
lngOutFile.Write strIn
lngOutFile.Close

Set lngInFile = Nothing
Set lngOutFile = Nothing
Set fso = Nothing



Then you can call the script like this:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Call ShellExecute(0, "Open", ("C:\Test.vbs"), "C:\Test.txt C:\TestNew.txt", "", 1)
End Sub
0
 
fds_fatboyCommented:
Using the ScriptControl, you can define function (and Sub) calls with parameters and call the individual functions with parameters. Also, you can trap errors in the script. So if your users get errors in their script your executable will know and can deal with them accordingly

Vinnyd79:
Can you trap errors using ShellExecute?
0
 
team2005Author Commented:
Hi!

I can't trap errors using shellExecute, how do i do that.

The code you send me regarding parameters, worked 100%. Thanks

You are the best.

Give you the points.

Tor
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now