How to automatically close an OK window in VBScript? - vbscript

I need to automatically close an "OK" window in a program by pressing Enter.
I tried to do that with a VBScript, but it gives me an error.
Set objshell = WshShell.Sendkeys "{Enter}" ("wscript.shell")
objshell.Run,0, True
exit
The error is:
Line: 1
Char: 34
Error: End of instruction expected
Code: 800A0401
Source: Microsoft Vbscript Compile Error

When in doubt, read the documentation. There is no intrinsic object WshShell in VBScript. You need to create a WScript.Shell instance and assign it to that variable before you can call the SendKeys method on it.
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{Enter}"
And you probably need to bring the window to which you're trying to send that keystroke to the front first using AppActivate.
Anyway, seeing how all of your 3 lines of code are entirely broken syntactically, I strongly recommend you go find a VBScript tutorial before proceeding any further.
Note also that SendKeys is a horribly unreliable method of automation. You may want to look into something like AutoIt instead.

Related

Single shift key in VBS

I'm trying to write a vbs script that has to send the shift key once. I dont need any other key pressed after it but I can't get it to work (I want to use only vbs not any other progarm nor another language). I've tried things like:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "+{}"
or
WshShell.SendKeys "+"
or
WshShell.SendKeys {+}
or
WshShell.SendKeys +
and many more but nothing seems to work.
Thank you in advance!
-Sandro Cutri
You can send a shift key (without anything else) with the Duck toolkit. It's a free webside. It "translates" your code from vbs to the ducky language (it turns it into a inject.bin file). I assume that that's what you want to do. From there you can write
SHIFT
That's all you need to write. It's important to write in CAPITAL letters.

How do I stop an auto typer from ‘pyramiding’?

I’ve been looking around for an auto-typer for some fun on Discord and I found a Virtual Basic code on Youtube that functions mostly to my needs but it has one problem. It’s supposed to type a phrase and then enter and then type and enter for a set amount of times but when I test it in notepad++ it does this:
test
testtest
testtesttest
testtesttesttest
And so on until it maxes out at 17. I’m not sure what could be wrong with it because I didn’t make it and I’m not very adept with coding at all. This is the code:
set shell = createobject(“wscript.shell”)
strtext = inputbox(“What would you like the message to be?”)
strtimes = inputbox(“How many times would you like to type it?”)
if not isnumeric(strtimes) then
lol=msgbox(“Please write a number next time”)
wscript.quit
end if
msgbox “After you click Ok the message will start in 2 seconds”
wscript.sleep(2000)
for i=1 to strtimes
shell.sendkeys (strtext & “”)
shell.sendkeys (“{Enter}”)
wscript.sleep(100)
next
I’ve tried adjusting spaces and such before and after parentheses but nothing seems to change the ‘pyramid’ outcome. Any suggestions on what I could try to change or if anyone actually sees what’s wrong would be much appreciated.
The Issue you are running into is with notepad++ specifically.
shell.sendkeys (“{Enter}”)
Is triggering the Word Auto Completion.
I ran this writing to standard Notepad without issue.
Edit:
This does work in Notepad++ if you do the Enter twice.
shell.sendkeys ("{Enter}")
shell.sendkeys ("{Enter}")
Or if you add a space after the string.
shell.sendkeys (strtext & " ")

CreateObject("WScript.Shell") is Nothing error

I have a script below:
Dim g_oWShell
Set g_oWShell = CreateObject("WScript.Shell")
But when I validate the value of g_oWShell first, that object is null (nothing). Below are the codes I'm debugging:
If g_oWShell Is Nothing Then
'display an error.
Else
'run Something... (Ex. g_oWShell.Run("...")
End If
I'm new to VBS and I'm not sure why is that the case. I have read a short definition of CreateObject method here: VBA CreateObject
Advance thanks for explaining what might I have missed.
EDIT:
I'm creating an MSI installer using Visual Studio.

Getting actual powershell return value in vb6?

Alright, so I have been trying to do this for a while, and I have come to the realization that this isn't really something that is often asked, and with vb6 getting phased out more and more, there seems to be less help than I would like regarding the language.
The title doesn't say it all actually, as I am looking to do something very specific. I need to execute a shell command (that I know how to do), however, after I execute it, I want to be able to save the return value of that command as a string. For example, if the command is ipconfig, I want the entire return value of that, all the text I would see in powershell after executing that command, saved to a string in my program.
As far as I know, I need to "import" a few things, because I have to use WshShell, which I don't know where to get. So that's part of the question, what classes do I have to add and how, or if there is a way to do it without adding classes then even better. In addition, I have heard a lot about the use of CreatePipe and such regarding similar problems, but I don't know how to use it.
Basically, what I'm saying is that I am quite uneducated regarding the subject, and any insight would be much appreciated, and thanks to all who reply.
There are many ways. Using VBScript's WSHShell.Exec is the easiest.
This is VBScript but VBScript can be pasted into VB (not vice versa though).
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("ipconfig")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
MsgBox oExec.StdOut.ReadAll
Slightly modified from help.
This is how to ping from VBS/VB
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
'msgbox colItems
For Each objItem in colItems
msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next

Why doesn't 'shell' work in VBscript in VS6?

In a macro for Visual Studio 6, I wanted to run an external program, so I typed:
shell("p4 open " + ActiveDocument.FullName)
Which gave me a type mismatch runtime error. What I ended up having to type was this:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
strResult = wshShell.Run("p4 open " + ActiveDocument.FullName)
What is going on here? Is that nonsense really necessary or have I missed something?
As lassevk pointed out, VBScript is not Visual Basic.
I believe the only built in object in VBScript is the WScript object.
WScript.Echo "Hello, World!"
From the docs
The WScript object is the root object of the Windows Script Host
object model hierarchy. It never needs to be instantiated before invoking its
properties and methods, and it is always available from any script file.
Everything else must be created via the CreateObject call. Some of those objects are listed here.
The Shell object is one of the other objects that you need to create if you want to call methods on it.
One caveat, is that RegExp is sort of built in, in that you can instantiate a RegExp object like so in VBScript:
Dim r as New RegExp
VBScript isn't Visual Basic.
Give this a try:
Shell "p4 open" & ActiveDocument.FullName
VB6 uses & to concatenate strings rather than +, and you'll want to make sure the file name is encased in quotes in case of spaces. Try it like this:
Shell "p4 open """ & ActiveDocument.FullName & """"

Resources