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.
asked Mar 21, 2017 at 3:52
1
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
answered Mar 21, 2017 at 9:19
Ansgar WiechersAnsgar Wiechers
193k25 gold badges256 silver badges330 bronze badges
3
VBScript has data (sub) types. Besides simple (sub) types like Strings:
>> x = "blank"
>> WScript.Echo VarType(x), TypeName(x)
>>
8 String
there are Objects:
>> Set y = New RegExp
>> WScript.Echo VarType(y), TypeName(y)
>>
9 IRegExp2
To assign an object to a variable, you need Set, to compare objects, you need Is. Simple (non-object) values have their own comparison operator.
To compare a string against another:
>> WScript.Echo CStr(x = "blank"), CStr(x = "object")
>>
True False
Trying to use a simple value ‘as if it were an object’, throws an «object required» error:
>> Set z = "blank"
>>
Error Number: 424
Error Description: Object required
>> WScript.Echo CStr(x Is x)
>>
Error Number: 424
Error Description: Object required
Read A Whole Lot Of Nothing.
Set objPath = objFSO.BuildPath(CurrentDirectory, """new 1.cmd""")
У вас есть 3 ошибки в этом утверждении:
- Вложенные двойные кавычки в
"""new 1.cmd"""
сделать их частью действительного имени файла, что недопустимо.FileSystemObject
методы могут обрабатывать пути с пробелами сами по себе, без дополнительных двойных кавычек. Вам нужно добавить двойные кавычки при прохождении пути кRun
метод, хотя. CurrentDirectory
является собственностьюWshShell
объекты, а не встроенная переменная.-
BuildPath
Метод возвращает строку, а не объект, поэтому вам нужно удалитьSet
ключевое слово.
Это будет делать то, что вы хотите:
objPath = objFSO.BuildPath(objShell.CurrentDirectory, "new 1.cmd")
...
objShell.Run """" & objPath & """", 1, True
Dim oFile, oFilesys,oFileADVFEEMAA, oFileADVFEE
set oFilesys = CreateObject(«Scripting.FileSystemObject»)
wscript.echo «Line 2»
set oFileADVFEEMAA = oFilesys.GetFile («D:\NDM\Upload\BPS_JE\BPSFile\ADVFEEMAA.txt»)
wscript.echo «Line 3»
set oFileADVFEE= oFilesys.GetFile («D:\NDM\Upload\BPS_JE\BPSFile\ADVFEE.txt»)
wscript.echo «Line 4»
set oFile = oFilesys. GetFileName(oFileADVFEE.Name) –“ Microsoft VBScript runtime error: Object required: ‘[string: «ADVFEE.txt»]’”—errror here
wscript.echo «File Name “ & oFile
if oFileADVFEEMAA.Size = 1 Then
oFileADVFEEMAA.Delete
wscript.echo «ADVFEEMAA file is Deleted.»
wscript.quit 0
Else
If oFileADVFEEMAA.Size>1 and oFileADVFEE.Size >1 Then
wscript.echo «Problem 2 ADVFEE files with data.»
wscript.quit 1
Else
If oFileADVFEEMAA.Size>1 and oFileADVFEE.Size = 1 Then
oFileADVFEE.Delete
oFileADVFEEMAA.Name = oFile
script.echo » ADVFEEMAA renamed to » & oFileADVFEEMAA.Name
wscript.quit 0
End If
End If
End If
What I have tried:
Dim oFile, oFilesys,oFileADVFEEMAA, oFileADVFEE
set oFilesys = CreateObject(«Scripting.FileSystemObject»)
wscript.echo «Line 2»
set oFileADVFEEMAA = oFilesys.GetFile («D:\NDM\Upload\BPS_JE\BPSFile\ADVFEEMAA.txt»)
wscript.echo «Line 3»
set oFileADVFEE= oFilesys.GetFile («D:\NDM\Upload\BPS_JE\BPSFile\ADVFEE.txt»)
wscript.echo «Line 4»
set oFile = oFilesys. GetFileName(oFileADVFEE.Name) –“ Microsoft VBScript runtime error: Object required: ‘[string: «ADVFEE.txt»]’”—errror here
wscript.echo «File Name “ & oFile
if oFileADVFEEMAA.Size = 1 Then
oFileADVFEEMAA.Delete
wscript.echo «ADVFEEMAA file is Deleted.»
wscript.quit 0
Else
If oFileADVFEEMAA.Size>1 and oFileADVFEE.Size >1 Then
wscript.echo «Problem 2 ADVFEE files with data.»
wscript.quit 1
Else
If oFileADVFEEMAA.Size>1 and oFileADVFEE.Size = 1 Then
oFileADVFEE.Delete
oFileADVFEEMAA.Name = oFile
script.echo » ADVFEEMAA renamed to » & oFileADVFEEMAA.Name
wscript.quit 0
End If
End If
End If
Remove these lines:
set oFile = oFilesys. GetFileName(oFileADVFEE.Name) –" Microsoft VBScript runtime error: Object required: '[string: "ADVFEE.txt"]'"---errror here wscript.echo "File Name " & oFile
and use the oFileADVFEE.Name directly:
wscript.echo "File Name " & oFileADVFEE.Name
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900
|
|