VBScript未终止的字符串常量

时间:2014-04-08 01:38:39

标签: windows-8 vbscript

我从网站上获取了这个脚本。我正在尝试向Windows 8上的任务栏和开始菜单添加快捷方式。我的VBScript知识非常薄弱。我很确定我只是错过了一个"在某个地方,但我不确定在哪里。第一个' String'在Else WScript.Echo之后,我的文本编辑器中没有显示为蓝色,因为它应该也是如此。任何帮助将非常感激。 (该错误声称是在5,60)

'Pin an application to a start menu or task bar
If WScript.Arguments.Count = 3 then
Call PinApplicationToTaskBar(WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2))
Else
WScript.Echo "Missing parameters. String AppPathAndName  _
String ShortcutName Boolean OnStartMenu." & vbCr & vbCr & "  _
Example cmd.exe CMD  false" & vbCr & vbCr & _
"  Example %windir%\system32\SnippingTool.exe SnipIt false" 
End If

Public Sub PinApplicationToTaskBar(AppPathAndName, ShortcutName, OnStartMenu)
'This is on for a soft failure. 
'Uncomment this if error checking for a hard failure is needed for debugging.
On Error Resume Next

Dim FileSystemObj, ObjShell, ObjShellApp
Set ObjShell = WScript.CreateObject("WScript.Shell")
Set FileSystemObj = CreateObject("Scripting.FileSystemObject")

'Create a temp location for the short-cut to exist
TempShortcutLocation = FileSystemObj.GetFolder_
(ObjShell.ExpandEnvironmentStrings("%TEMP%"))
'Where is it being pinned too?  Determine the location where the pinned item will reside.
If(trim(lcase(OnStartMenu)) = "true") then ' pinned to start menu
    HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
    ("%APPDATA%") & _
    "\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu"
Else
    HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
    ("%APPDATA%") & _
    "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
End If
'Temporary location for the application short-cut
TempShortcut = TempShortcutLocation & "\" & ShortcutName & ".lnk"
'Possible location of a pinned item
HasItAlreadyBeenPinnedShortCut =  HasItAlreadyBeenPinnedShortCut & "\" & ShortcutName & ".lnk"
'If this already exists, than exit this procedure. The application has already been pinned.
If(FileSystemObj.FileExists(HasItAlreadyBeenPinnedShortCut)) Then
    'MsgBox(HasItAlreadyBeenPinnedShortCut & " Already Pinned")
    Set ObjShell = Nothing
    Set FileSystemObj = Nothing
    Exit Sub
End If
'Create a short-cut using the shell
Set lnk = ObjShell.CreateShortcut(TempShortcut)
lnk.TargetPath = AppPathAndName ' Full application path and name
lnk.Arguments = ""
lnk.Description = ShortcutName 'The name that appears on the start menu.
lnk.Save 

Set ObjShellApp = CreateObject("Shell.Application")

'Get the newly created short-cut full path
Set ShortCutToPin =  ObjShellApp.NameSpace(TempShortcutLocation) 

If(FileSystemObj.FileExists(TempShortcut)) Then 
    Dim ShortCutToPinItem, verb
    'Set the location to pin the item to do based on the passed OnStartMenu argument
    If(trim(lcase(OnStartMenu)) = "true") then
        verbToDo = "Pin to Start Men&u"
    Else    
        verbToDo = "Pin to Tas&kbar"
    End If
    For Each ShortCutToPinItem in ShortCutToPin.Items()
        'Look for the pinning verb when the temporary short-cut name matches the passed ShortcutName argument
        If (ShortCutToPinItem.Name = ShortcutName) Then
            'Loop through the shell object's (the short-cut) commands looking for the pinning method.
            For Each verb in ShortCutToPinItem.Verbs 
                'The verb matches the verbToDo so pin it to verb's defined location
                If (verb.Name = verbToDo) Then verb.DoIt
            Next
        End If
    Next
    'Delete the temporary short-cut used to pin the application
    FileSystemObj.DeleteFile(TempShortcut) 
End If
'clean up
Set ObjShell =  Nothing
Set FileSystemObj = Nothing
Set ObjShellApp = Nothing
End Sub

为了参考和信用,代码来自这里。 http://www.codeproject.com/Tips/713824/Pin-a-shortcut-onto-the-Taskbar-or-Start-Menu

1 个答案:

答案 0 :(得分:2)

这个字符串连接搞砸了。它应该是这样的:

WScript.Echo "Missing parameters. String AppPathAndName " & _
             "String ShortcutName Boolean OnStartMenu." & vbCr & vbCr & _
             "Example cmd.exe CMD false" & vbCr & vbCr & _
             "Example %windir%\system32\SnippingTool.exe SnipIt false" 

您必须同时使用行继续(_)和字符串连接(&)运算符来连接VBScript中多行的字符串。确保所有字符串文字都已关闭(每个引号在同一行上都有匹配的结束引号)。