Reputation: 131
Option Explicit
Dim objShell, intRetVal, intErrNum
Dim sn As String
Set objShell = CreateObject("WScript.Shell")
objShell.Run("c:\Windows\System32\notepad.exe")
WScript.Sleep 2000
sn = f
If objShell.AppActivate("Untitled - Notepad") Then
WScript.Sleep 500
objShell.SendKeys "%{sn}"
End If
In the above vbs code I am trying to pass alt+f Key to "untitled - Notepad" application but instead of passing key directly I wanted to assigned it to some variable and then pass the variable.
But above code is throwing below error while executing the script,
E:\vbs\test2.vbs(3, 8) Microsoft VBScript compilation error: Expec ted end of statement
How should I assigned the key to variable and pass variable as key.
Upvotes: 0
Views: 4223
Reputation: 200193
There are 3 mistakes in your script:
Dim sn As String
As Adrien Lacroix already pointed out, VBScript doesn't support typed variable declarations as VBA does. Change that line into this:
Dim sn
sn = f
You want to assign the character f
to the variable sn
, so you have to make the former an actual string by putting it between double quotes:
sn = "f"
objShell.SendKeys "%{sn}"
VBScript doesn't support variable expansion inside strings. If you want to build a strings with values from variables you have to concatenate the variable with the string literals. Also, the curly braces are for generating special keystrokes (e.g. {Right}
for → or {F10}
for F10), not for variable expansion. See the documentation for details. To send Alt+F you need the following:
objShell.SendKeys "%" & sn
Upvotes: 2
Reputation: 3612
I think your error is on this line (E:\vbs\test2.vbs(3, 8)) :
sn = f
provoked by this one :
Dim sn As String
From MSDN : "In VBScript, variables are always of one fundamental data type, Variant"
VBScript does not support data types as such so remove all data types from your declarations. For example, that line should be just:
Dim sn
Upvotes: 1