How to execute VBS? - vbscript

I found how to make this
Download_Example
I have a question about how to make execute VBS in vb6 (VBS haves form3 (from vb6 project .)show)
I made a dialog with Microsoft common dialog control 6.0
CommonDialog1.Filter = "File (*.vbs)|*.txt|All Files (*.*)|*.*"
CommonDialog1.DefaultExt = "vbs"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
The FileName property gives you the variable you need to use

A work-around might be just executing the script using Shell.
Shell "wscript.exe c:\myscript.vbs", vbNormalFocus
Shell "wscript.exe " & CommonDialog1.FileName, vbNormalFocus
See Microsoft's wscript documentation.
vbNormalFocus is there to restore focus to your vb6 program. It is optional but you probably want it. See documentation.

Looks like you are trying to run a VBScript from your VB6 app to open a dialog in the VB6 app.
VB6 -> VBScript -> Same VB6
You cannot do this with Shell since it runs the script as a separate process. The Script does not know what Form3 is because it is a component of the VB6 app and would not exist as a separate entity once the app is compiled.
Edit: Looks like what you want to do is possible but with Microsoft Script Control. Here are a few examples. Thank you #GSerg for pointing this out.
This or this might be used as a work-around but I don't think it is the right way to go.
Go back to your requirements. What exactly are you trying to accomplish? There has to be a better way.

Related

How to close an error message from an .exe called from a Excel-VBA script?

When calling a external .exe file from vba, like this:
Sub RunEXE()
Dim wsh As Object
SetCurrentDirectory "\\path\" 'Set Directory
Set wsh = VBA.CreateObject("program.exe ""\\path\argument""", WindowStyle:=1, waitonreturn:=True)
Is it possible to close error windows directly from VBA?
The code calls the program and correctly runs the .exe and produces all of the data that is useful to me, but to get the data back to my sheet I have to okay this window (since waitonreturn=true):
The violation appears to be something I can't impact from my VBA code, but it is not impacting the section of the .exe I need in order to do the calculations required by my code. Instead of delving into the world of memory access violation of an external piece of software, I am hoping to side step the problem by okaying it direct from the VBA code.
If I click okay on that error message everything is perfect as far as my code is concerned. I don't need the .exe to save the files causing the error, and I already have the data I need.
Is it possible to dismiss an error window from external software from my excel-VBA code?
NOTE: The error comes from the .exe program, not from excel itself.
This is a simple example of sendkeys, which will open notepad and then send an Enter key:
Sub SendEnterToNotepad()
With Application
Shell "Notepad.exe", 3
SendKeys "{ENTER}"
End With
End Sub

Command-line to show a file's properties?

I want to write an app that displays the default Windows Properties sheet, I can't find much info on doing it the normal programmatic way, so I'm thinking maybe shell out to a command-line call; does anyone know the Windows command-line to call up a file's property/details window? Not having any luck searching for it.
This window here:
In a general sense, there are two methods for going about this. How you specifically accomplish it (i.e. PowerShell, C#, etc) is up to you.
Call the ShellExecute function on the file with the PROPERTIES verb.
Call the SHObjectProperties function on the file.
Examples of both can be found here in AutoIt: http://www.autoitscript.com/forum/topic/118673-open-a-files-properties-window/
It appears that there IS no direct command-line call that can do this, so I whipped up a quick little VBScript to do it, since it appeared to be the quickest and easiest way to achieve the end result (at least for my specific need).
The VBScript simply sends the Windows Shortcut Alt + Enter; so, when a file is highlighted it'll automatically use that file as the one to send the shortcut to.
For anyone else who may want to do the same, just paste the following into a new text file and save it as a .vbs file:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%{ENTER}"
For anyone interested in an even better solution, I ended up writing a small command-line app in VB.NET to do this, here's a link to it's full info + the source code: https://geekdrop.com/x/props

How to comfortably monitor variables in a VBscript during development process? (e.g. in a continuously opened command window)

I need to write a huge VBscript to automatically run an application and I'm looking for a way to comfortably monitor what I'm actually doing, in other words, to display the values of some/all variables involved in my script.
I'm used to work with Matlab, where I have a comfortable workspace browser. When I run a Matlab script, all variables, their types and their values are accessible in that workspace and can be checked.
The VBscript I write with Notepad++ (it needs to be a free editor) and the only way I found to display variables was echoing them via wscript and cscript.
I set up the shortcuts.xml with the following line to run my script directly from Notepad++:
<Command name="Run with CScript" Ctrl="yes" Alt="no" Shift="yes" Key="116">cmd /K %windir%\system32\cscript.exe "$(FULL_CURRENT_PATH)"</Command>
In case I include commands in my script like
Wscript.Echo myVar
Wscript.Echo "Hello World!"
and run it with the newly introduced shortcut, a cmd window pops up and displays the value of myVar and "Hello World!".
But the next time I run the script a new window pops up. So my question is:
Is it possible get a continuously opened output window, displaying all echoed values everytime I run a script? I actually want to put the window on a second screen and keep the values from previous runs. So I can enter a line Wscript.Echo something, run, check, enter something else and so on, without fiddling around with a bunch of opened windows.
Alternatively, is there any open-source/free editor which offers an accessible workspace like the one in Matlab?
The open-source editor SciTE offers what I was looking for.
The default settings in vb.properties enable a similar behavior like in Notepad++
command.build.$(file.patterns.wscript)=cscript "$(FilePath)"
command.build.subsystem.$(file.patterns.wscript)=1
One can change it as follows to get the output into the integrated console.
command.go.$(file.patterns.wscript)=cscript.exe //nologo "$(FilePath)"
command.go.subsystem.$(file.patterns.wscript)=0
F5 runs the script and Shift+F5 cleans the output.
Another option is the NppExec Plugin for Notepad++ suggested by #Ansgar Wiechers, which adds a console. The script can be run with cscript.exe /nologo "$(FULL_CURRENT_PATH)" then.
Use a debugger. Start your script with the (meta)option //X. If you are lucky, you already have installed software (MS Office, Visual Studio (Express)) that provides a debugger for VBScript. If not do a bit of research to find an Express version suitable to your OS.
You can almost write native VBScript in the VBA editor, so if you have Excel or whatever you can use this to debug, then go through some steps to convert back to VBScript. That's what I usually do.

Creating and running a Vbs Script VIA a windows form

I have a Vbs script to install network printers at my place of work. The only issue is that it needs to be hard coded with the printer and server name every time I want to use it. So as a solution I was trying to figure out if I could make a simple Windows form with a text box or drop down list that would simply push the input from there into the vb script and then run it. I can't figure out how to do this for the life of me though. Here is my VB script Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\wcprt02\S100a"
'objNetwork.SetDefaultPrinter "\wcprt02\L30b"
Your options are:
Use WScript.Arguments to pass (a few) parameters to your script started with cscript.exe from a console (DOS Box)
Read the arguments from a file (maintained via the editor of your choice; or Excel if you prefer)
Use InputBox to gather (a few) parameters via simple dialogs/pop-ups
Write a .HTA application presenting a GUI for (maybe many) arguments; pass them to your code embedded as a function/sub in the .hta
Write a .NET Forms application presenting such a GUI; you could then shell out to your script, but why not stay in .NET and avoid the mixing of technologies?

How to show a popup without a browser

I need an "alert" type feature to troubleshoot an error. I am not using a browser and using javascript as windows administaration purposes. So is their a way to view a varibales value if I am not using a browser?
JScript is a scripting language based on the ECMAScript standard.
JScript is implemented as a Windows Script engine. This means that it can be plugged in to any application that supports the Windows Script host, such as Internet Explorer, Active Server Pages, etc. It also means that any application supporting Windows Script can use multiple languages — JScript, VBScript, Perl, and others.
For reasons that I am not sure about, but I believe it to be related to the fact the the DOM is not available outside the browser, the alert function is also not available outside the browser. In order to popup a dialog box to the user in this case you can use the following code:
WScript.Echo('The quick brown fox jumped over the lazy dog');
If you want a windows GUI popup, then:
var timeout = 0;
var buttons = 0; // OK
var icon = 48; // Exclamation
var shell = new ActiveXObject("WScript.Shell");
shell.Popup("text ...", timeout, "window title", buttons + icon);
and run your jscript program with the wscript command.
Microsoft JScript language reference.
Popup documentation.
On windows, you can use Windows Script Host to execute your javascript. It has a built in ability to do output, using Echo. There are some nuances though, since WSH uses jscript, not javascript, though the languages are similar.
A summary of the differences between WScript.Echo and WshShell.Popup:
Windows scripts (vbs, js, wsf etc.) can be run under one of two hosts: cscript.exe (command-line), and wscript.exe (graphical). Under cscript, WScript.Echo will produce a line of text in the console window. WshShell.Popup will always produce a message window, even under cscript.
WshShell.Popup lets you specify the buttons, title and icon type, like the VB/VBS MessageBox function. It also lets you specify how long the message should remain open.
WScript.Echo lets you pass multiple string arguments to output, and will print them separated with spaces.
You can create a simple file that will alert text that is passed to it, for example in python. I don't think there is any way to do this in Javascript though without a browser.
No with javascript. You can, using Visual Basic Script and MsgBox function. No need to install anything.
'In Hello.vbs. Comments starts with '
MsgBox "Hello there"
Look at HTA files. These file types allow you to run typical HTML/VBScript/JS code without the need for a browser specifically. Just rename your HTML file to an HTA extension and run it. IT will show your "page" and execute any JS necessary. This type of file will give you access to other WScript functions as well like creating Files or accessing AD if required.

Resources