EternalMonologue
EternalMonologue

Reputation: 41

VBS Object required: '[string: ""]'

I have the code

set WshShell = CreateObject("WScript.Shell")
Set ie = CreateObject("InternetExplorer.Application")
ie.Offline = True
ie.Navigate "about:blank"
ie.Height     = 200
ie.Width      = 425
ie.MenuBar    = False
ie.StatusBar  = False
ie.AddressBar = False
ie.Toolbar    = False
ie.Visible = True
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WScript.Sleep 3000
set a = WshShell.SendKeys("a") & Wscript.Sleep("100")
a

It does type "a" in notepad, but then it gives the error," Object Required: '[string: ""]' " and it will prevent any code after it from running.

If anyone knows how to fix this and prevent it in the future that would be great.

Upvotes: 0

Views: 3786

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You're trying to assign something to a variable as an object that isn't an object (Set a = ...). Don't do that. Neither SendKeys() nor Sleep() return output, so there's no point in assigning that non-output anyway. Or in concatenating it (you're probably confusing the VBScript string concatenation operator & with the batch command chaining operator &).

Change this:

Set a = WshShell.SendKeys("a") & WScript.Sleep("100")

into this:

WshShell.SendKeys("a")
WScript.Sleep(100)

and the problem will disappear.


If you're trying to implement a procedure that you can invoke as a shorthand, that would be done e.g. like this:

Sub k
  WshShell.SendKeys("a")
  WScript.Sleep(100)
End Sub

k  'sends keystroke "a" and waits 100 ms

or like this, if you want it parametrized:

Sub k(keys)
  WshShell.SendKeys(keys)
  WScript.Sleep(100)
End Sub

k "b"  'sends keystroke "b" and waits 100 ms

Upvotes: 1

Related Questions