Getting : Microsoft VBScript compilation error: Expected ')'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Getting : Microsoft VBScript compilation error: Expected ')'



I am new to vb script and want to create a shortcut for my .exe application which will open in an invisible window. Here is the code which I have written


Option Explicit

Private Sub Command1_Click()
'This will Create a ShortCut of test_application in our desktop, its name is "My-Test", invisible windows when run, use the 2nd icon as the Shortcut icon.'

Create_ShortCut "C:MyAppbintest_application.exe", "Desktop", "My-Test", , 0, 1
End Sub

Sub Create_ShortCut(ByVal TargetPath As String, ByVal ShortCutPath As String, ByVal ShortCutname As String, Optional ByVal WorkPath As String, Optional ByVal Window_Style As Integer, Optional ByVal IconNum As Integer)

Dim VbsObj As Object
Set VbsObj = CreateObject("WScript.Shell")

Dim MyShortcut As Object
ShortCutPath = VbsObj.SpecialFolders(ShortCutPath)
Set MyShortcut = VbsObj.CreateShortcut(ShortCutPath & "" & ShortCutname & ".lnk")
MyShortcut.TargetPath = TargetPath
MyShortcut.WorkingDirectory = WorkPath
MyShortcut.WindowStyle = Window_Style
MyShortcut.IconLocation = TargetPath & "," & IconNum
MyShortcut.Save

End Sub



I am stored the script as test.vbs and executed it in the following way


C:UsersmeDesktop>cscript test.vbs



and it gave me the following error


Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

C:UsersmeDesktoptest.vbs(9, 38) Microsoft VBScript compilation error: Expected ')'



Is this a good way to create a shortcut or is there any better and more verbose way to do it ?




1 Answer
1



There are a few problems with your script.



In answer to your question, the reason you are receiving an error is because VBScript only supports one data type - Variant. In your function "Create_Shortcut", you are defining your parameters as particular data types, such as "As String and "As Integer". Remove the data type declarations, and you've fixed your problem - kind of.



The next problem is that VBScript doesn't support optional parameters. So, you need to remove the Optional keyword in your "Create_Shortcut" method signature also. Ultimately, the method signature will look like this:


Private Sub Create_ShortCut(TargetPath, ShortCutPath, ShortCutname, WorkPath, Window_Style, IconNum)



Another concern I have about this script is that it looks like it is handling for a button click (Private Sub Command1_Click); if this is a VB Script and not a VB 6 application, you don't need the button click handler. You do, however, need to call your function, so if you remove the signature for the button click as well as the closing "End Sub", you will be calling your function properly. However....



The code in your "Create_Shortcut" method has a problem also. Just as in the description above, there is only one data type - Variant - so remove the "As Object" from the two lines declaring variables.



The function still does not work, but this last problem is because you are passing in an empty working directory path when calling the method; the working directory is required, so just be sure to pass it to your method. Change your code from:


Create_ShortCut "C:MyAppbintest_application.exe", "Desktop", "My-Test", , 0, 1



to


Create_ShortCut "C:MyAppbintest_application.exe", "Desktop", "My-Test", "C:MyAppbin" , 0, 1



So, ultimately, your VBS file will look like this:


Create_ShortCut "C:MyAppbintest_application.exe", "Desktop", "My-Test", "C:MyAppbin" , 0, 1

Private Sub Create_ShortCut(TargetPath, ShortCutPath, ShortCutname, WorkPath, Window_Style, IconNum)
Dim VbsObj
Set VbsObj = CreateObject("WScript.Shell")

Dim MyShortcut
ShortCutPath = VbsObj.SpecialFolders(ShortCutPath)
Set MyShortcut = VbsObj.CreateShortcut(ShortCutPath & "" & ShortCutname & ".lnk")
MyShortcut.TargetPath = TargetPath
MyShortcut.WorkingDirectory = WorkPath
MyShortcut.WindowStyle = Window_Style
MyShortcut.IconLocation = TargetPath & "," & IconNum
MyShortcut.Save
End Sub





That worked now what should I do if my .exe application takes some input parameters. for example Create_ShortCut "C:MyAppbintest_application.exe localhost 1500 -exec="Excute this"", "Desktop", "My-Test", "C:MyAppbin" , 0, 1
– msd_2
Sep 19 '13 at 17:37






You set the arguments for the shortcut using the Arguments property, such as: MyShortcut.Arguments = "some args"
– Jay Taplin
Sep 19 '13 at 17:45





Thank you that worked...but even though I specified the Window style to be 0 (Invisible), the shortcut created opens the window normally. Is there any diiferent way by which the window can be made to open invisibly
– msd_2
Sep 19 '13 at 17:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS