VB (visual basic) probably silly questions from the newbie

OK, I’m finally futzing around with VB. (Don’t laugh at the silly Mac person’s first foray into VB…we don’t have the stuff around much in MacOS…)

I have this in a VB editing window within MS Word:

Sub SayHello ()
Result = MsgBox (“Click a Buitton”, vbYesNoCancel, “A Message”)
MsgBox Result
End Sub

Anyone with a copy of Visual Basic for Dummies will recognize that :wink:

Anyway, I hit the run button and it makes dialogs.

Then I put the exact same text inside of Notepad and save it to my desktop as a file “hello.vbs”.

It shows up with a little scroll icon.

But when I double-click it, I don’t get a dialog. Just a second or two of hourglass.
What I’m up to here — to provide a bit of backstory —is finding out how I can extend the reach of FileMaker Pro on the Wndows platform. I was told (correctly or incorrectly) that I could assemble a series of records each of which would contain a line of vb in a text field, then export those records as a text file ending in “.vbs” and then turn around and execute it. So the first order of business is to get a code snipped that works as a stand-alone .vbs file and then export the same from FmPro.

Was I misinformed? Or is there some kind of initial statement I need to make before “Sub” if the vb code is in a standalone “vbs” file instead of in the VB editor of an MS Office app?

In your vbs file only store these lines of code:

Result = MsgBox (“Click a Buitton”, vbYesNoCancel, “A Message”)
MsgBox Result

Khadaji has the good practical advice. Here’s the theoretical stuff behind it.

MSword VBA (the stuff that you write in the word ‘editing window’) is a variant of visual basic 6, and inherits the rules that all code should be in a sub or a function. A sub with no arguments, like your sayHello, becomes a ‘runnable macro’ and will execute if you press the ‘go’ button while your cursor is inside it.

.vbs scripts, obviously, are visual basic scripting edition, like active server pages, and have slightly different rules. Here, there can be active code outside of subs and functions, and in fact, these are the only routines that run by default. You can still write a sub in .vbs, but you have to call it yourself, and you have to call at least one sub/function from outside all of them, or none of them will start running.

Is that clear as mud? :slight_smile:

**Chrisk **- that was a great explanation. To extend it then with Ahunter3’s code, the example would look like this:

SayHello
Sub SayHello ()
Result = MsgBox (“Click a Buitton”, vbYesNoCancel, “A Message”)
MsgBox Result
End Sub

Save that to your vbs file and try to run it.

One more gotcha is that VBS script outside of Office is run by the Windows script host (WSH), which comes in two flavors, wscript.exe & cscript.exe. wscript runs the scripts as windows GUII apps, while cscript runs them as Cmd=console=DOS-style scripts.

The default setup is that when you double-click a .vbs file, wscript launches to execute it as a GUI app. If that got changed so cscript is the default, double clicking a vbs file will open a black command window then run the script in it, with msgbox output being typed out onto the screen.

If you experience that effect, enter “wscript /H:WScript” in the Start | Run … window to reset the default host to GUI mode. Your user has to be an admin on the box to change the default host.

I’m not familiar with VBS, but if it’s processing the document as written it’ll hit the “SayHello” call and say “I don’t know what SayHello is”. If that fails, try putting SayHello below the End Sub line.

Naw. I’m a developer. I test my code before publishing. It works the way I wrote it.

Repeat after me:
A browser is NOT the same thing as a programming environment. An HTML document is NOT equivalent to a program.
A browser is NOT the same thing as a programming environment. An HTML document is NOT equivalent to a program.
A browser is NOT the same thing as a programming environment. An HTML document is NOT equivalent to a program.
A browser is NOT the same thing as a programming environment. An HTML document is NOT equivalent to a program.


What does that have to do with anything? I’ll give you the benefit of the doubt and assume that you were not trying to insult me, though what you were trying to do I’m not sure.

The example I was thinking of specifically was C++. Due to the way compilation happens, similar code in C++ would fail.

I believe that vbscript is done with a two-pass interpreter of sorts, which among other things, ‘hits’ all subroutines and compiles them before actually beginning to execute code.