Visual Basic脚本无法启动

时间:2015-07-23 18:12:04

标签: windows batch-file vbscript

我有一个带有相应.bat文件的VB脚本,该文件将其放在" C:\ Users \ Default \ AppData \ Roaming \ Microsoft \ Windows \ Start Menu \ Programs \ Startup" 夹。但是,在启动时,脚本无法完全运行。该脚本应该创建任务栏快捷方式,但不会为我列出的Office产品制作它们。

这是脚本:

    Option Explicit

Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PROGRAMS = &H2
Const CSIDL_STARTMENU = &HB
Const CSIDL_CURRENT_USER_DESKTOP = &H10
Const CSIDL_PUBLIC_DESKTOP = &H19

Dim objShell, objFSO

Dim objCurrentUserStartFolder
Dim strCurrentUserStartFolderPath

Dim objCurrentUserDesktopFolder
Dim strCurrentUserDesktopFolderPath

Dim objAllUsersProgramsFolder
Dim strAllUsersProgramsPath

Dim objPublicDesktopFolder
Dim strPublicDesktopFolderPath

Dim objFolder
Dim objFolderItem
Dim colVerbs
Dim objVerb

Set objShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objCurrentUserStartFolder = objShell.NameSpace (CSIDL_STARTMENU)
strCurrentUserStartFolderPath = objCurrentUserStartFolder.Self.Path

Set objCurrentUserDesktopFolder = objShell.NameSpace (CSIDL_CURRENT_USER_DESKTOP)
strCurrentUserDesktopFolderPath = objCurrentUserDesktopFolder.Self.Path

Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path

Set objPublicDesktopFolder = objShell.NameSpace (CSIDL_PUBLIC_DESKTOP)
strCurrentUserDesktopFolderPath = objPublicDesktopFolder.Self.Path

' - Remove pinned items -

'Internet Explorer
If objFSO.FileExists(strCurrentUserStartFolderPath & "\Programs\Internet Explorer.lnk") Then
    Set objFolder = objShell.Namespace(strCurrentUserStartFolderPath & "\Programs")
    Set objFolderItem = objFolder.ParseName("Internet Explorer.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
    Next
End If

'Google Chrome
If objFSO.FileExists(strAllUsersProgramsPath & "\Google Chrome\Google Chrome.lnk") Then
    Set objFolder = objShell.Namespace(strAllUsersProgramsPath & "\Google Chrome")
    Set objFolderItem = objFolder.ParseName("Google Chrome.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
    Next
End If

'Windows Explorer
If objFSO.FileExists(strCurrentUserStartFolderPath & "\Programs\Accessories\Windows Explorer.lnk") Then
    Set objFolder = objShell.Namespace(strCurrentUserStartFolderPath & "\Programs\Accessories")
    Set objFolderItem = objFolder.ParseName("Windows Explorer.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
    Next
End If

'Windows Media Player
If objFSO.FileExists(strAllUsersProgramsPath & "\Windows Media Player.lnk") Then
    Set objFolder = objShell.Namespace(strAllUsersProgramsPath)
    Set objFolderItem = objFolder.ParseName("Windows Media Player.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
    Next
End If

' - Pin to Taskbar -

'Mozilla Firefox
If objFSO.FileExists("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Mozilla Firefox.lnk") Then
    Set objFolder = objShell.Namespace("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\")
    Set objFolderItem = objFolder.ParseName("Mozilla Firefox.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
    Next
End If

'Windows Explorer
If objFSO.FileExists(strCurrentUserStartFolderPath & "\Programs\Accessories\Windows Explorer.lnk") Then
    Set objFolder = objShell.Namespace(strCurrentUserStartFolderPath & "\Programs\Accessories")
    Set objFolderItem = objFolder.ParseName("Windows Explorer.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
        If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
    Next
End If

'Microsoft Office Word
If objFSO.FileExists("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\Word 2013.lnk") Then
    Set objFolder = objShell.Namespace("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\")
    Set objFolderItem = objFolder.ParseName("Word 2013.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
            If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
    Next
End If

'Microsoft Office Excel
If objFSO.FileExists("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\Excel 2013.lnk") Then
    Set objFolder = objShell.Namespace("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\")
    Set objFolderItem = objFolder.ParseName("Excel 2013.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
            If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
    Next
End If

'Microsoft Office Powerpoint
If objFSO.FileExists("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\PowerPoint 2013.lnk") Then
    Set objFolder = objShell.Namespace("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office 2013\")
    Set objFolderItem = objFolder.ParseName("PowerPoint 2013.lnk")
    Set colVerbs = objFolderItem.Verbs
    For Each objVerb in colVerbs
            If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
    Next
End If

' - Remove IE shortcuts from start menu -

'Internet Explorer
If objFSO.FileExists(strCurrentUserStartFolderPath & "\Programs\Internet Explorer.lnk") Then
    objFSO.DeleteFile(strCurrentUserStartFolderPath & "\Programs\Internet Explorer.lnk")
End If

'Internet Explorer 64-bit
If objFSO.FileExists(strCurrentUserStartFolderPath & "\Programs\Internet Explorer (64-bit).lnk") Then
    objFSO.DeleteFile(strCurrentUserStartFolderPath & "\Programs\Internet Explorer (64-bit).lnk")
End If


' - Remove Thunderbird from current user desktop -
If objFSO.FileExists(strCurrentUserDesktopFolderPath & "\Mozilla Thunderbird.lnk") Then
    objFSO.DeleteFile(strCurrentUserDesktopFolderPath & "\Mozilla Thunderbird.lnk")
End If

' - Remove Thunderbird from public desktop -
If objFSO.FileExists(strPublicDesktopFolderPath& "\Mozilla Thunderbird.lnk") Then
    objFSO.DeleteFile(strPublicDesktopFolderPath& "\Mozilla Thunderbird.lnk")
End If

' - Remove Google Chrome from current user desktop -
If objFSO.FileExists(strCurrentUserDesktopFolderPath & "\Google Chrome.lnk") Then
    objFSO.DeleteFile(strCurrentUserDesktopFolderPath & "\Google Chrome.lnk")
End If

' - Remove Google Chrome from Public desktop -
If objFSO.FileExists(strPublicDesktopFolderPath & "\Google Chrome.lnk") Then
    objFSO.DeleteFile(strPublicDesktopFolderPath & "\Google Chrome.lnk")
End If

'Delete the script
DeleteSelf

Sub DeleteSelf()
        Dim objFSO
        'Create a File System Object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Delete the currently executing script
        objFSO.DeleteFile WScript.ScriptFullName
        Set objFSO = Nothing
End Sub

这是.bat文件:

mkdir "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
copy /y SetShortcuts.vbs "C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

任何想法为什么这都不起作用?

答案:我明白了。我的文件路径名称错误,因为如果计算机上安装了以前版本的Office,则路径会有所不同。

2 个答案:

答案 0 :(得分:1)

Default User profile is a template profile for all created users. Whenever you create a new user profile, the profile is built based on the Default User profile.

Copy the file to "%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Startup" instead.

答案 1 :(得分:0)

答案:我明白了。我的文件路径名称错误,因为如果计算机上安装了以前版本的Office,则路径会有所不同。