Solved

VBS include or import

Posted on 2006-11-17
16
2,518 Views
Last Modified: 2013-11-18
I'm writing some VBS for the SecureCRT tool and want to better organize my code.

I have a set of constants that I want to use in a series of vbs files, and rather than copy/paste them into each, I'd like to include the code through a single "include" statement.

What command or set of commands do I use to achieve this?

e.g.
Import "KeyboardConstants.vbs"

Sub Main()
  MsgBox "Here's a carriage return:" & ReturnKey & "!!!"
End Sub

---
When I run this, I'l like it to show a messagebox.
ReturnKey would be a variable defined inside the KeyboardConstants.vbs file.

I may also have to procedures in that import file which would be available with a simple call such as this:

Sub Main()
  RunExternalProcedure()
End Sub

where RunExternalProcedure() is defined in KeyboardConstants.vbs
0
Comment
Question by:rspahitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 17968013
Onfortunatelly this is not supported in vb script - You have to repeate your declaration in each file
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17968017
Unfortunately*
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17968040
So what is ExecuteGlobal?  It seems to be a way to import code, but I'm just reading up on it. Might that help?
0
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!

 
LVL 22

Author Comment

by:rspahitz
ID: 17968047
It seems that it lets you take code and run it...if I can read the code file, I may be able to run it.  Have you ever tried that?
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17968058
Actually, I found this in the archives and it seems to be doing that:

'Sub Include(sCodeFile)
'      Dim sCode
'
'      with createobject("scripting.filesystemobject")
'            sCode = .OpenTextFile(sCodeFile).ReadAll
'      End With
'      ExecuteGlobal sCode
'End Sub

Now I think I understand how it works...I'll experiment a bit after lunch, otherwise on Monday.
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17968067
Hmm I never used it. Let me know if it works :)
0
 
LVL 10

Accepted Solution

by:
Clif earned 175 total points
ID: 17968169
Actually it can be done, but you have to create WSF files, instead of VBS (the include file can be a VBS file)

Test.wsf:
<job>
<script language="VBScript" src=".\Constants.vbs"/>
<script language="VBScript">
  call TryIt()
  wscript.echo "xyz = " & xyz '"This is a test"
</script>
</job>

Constants.vbs:
dim xyz
xyz = 123

sub TryIt()
  wscript.echo "Try It"
end sub
0
 
LVL 13

Assisted Solution

by:Michael_D
Michael_D earned 75 total points
ID: 17968225
>>Actually it can be done, but you have to create WSF files, instead of VBS (the include file can be a VBS file)

Or you can use Java, PHP, C/C++ or any other language that suports includes :)

The question is how to do it using VBScript, right?
Just my opinion ...
0
 
LVL 10

Expert Comment

by:Clif
ID: 17968351
Michael_D,
"The question is how to do it using VBScript, right?"

By adding the tag:
    <script language="VBScript">
 
You will be using VBScript.

The script will still be working within the WScript engine.

Try it.
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17968387
I mean if i am coding in VBS and want to include functions or constants from other site - I will need pure VBS solution to do it - not creating wrapper or launcher in other language
0
 
LVL 10

Expert Comment

by:Clif
ID: 17968442
VBS need an engine to run the script.

What engine are you running that "pure VBS solution" in?

The "wrapper or launcher" is not another language.  It's a directive to tell the scripting engine what to do.
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17968764
The problem I have is that this is not a web project.  The SecureCRT app uses VBScript as a macro language, but it seems to use a limited subset.

I can actually test the syntax in Excel VBA for the most part, but not everything works.

I'll check out the WBS concept.

FYI
The VBS files start with this:

# $language = "VBScript"
# $interface = "1.0"

Then you add a sub-main and the code to run.  The problem is that the file is getting kinda big and I'd like to break it up into modules.  I have it load a menu into an InputBox, then jump to a subroutine based on the input.  (I was hoping to add a custom form, but in CreateObject I wasn't sure what class to instantiate.  But that's for another question if I can get this working.
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17968836
Well, this works !!! :)

      ExecuteGlobal "Sub x:Msgbox ""x"":end sub"
      x()

That's kinda cool.
However, setting a bunch of constants in a separate file generates an error in the ExecuteGlobal.
I might be able to put them elsewhere, like a class of some sort.  I'll have to keep playing.


And I like the idea of a Windows Script File (WSF) but I don't think it will work with the SecureCRT app.  Maybe when I start doing webpages (although I usually use JavaScript for that since it's more portable to most browsers.)
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17968847
OK...got it...the external file had the heading directives in it (# $lang...)  When I took those out, it worked! :)

I'll keep this open for a bit in case there are any other comments.
0
 
LVL 22

Author Comment

by:rspahitz
ID: 17973592
Although I actually got the answer myself, I think that talking it out helped guide me in the right direction.  I also learned a few things about vbscript---so thanks.
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17973624
rspahitz thank you for points and for good question / answer
I learned also something new to me :)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question