vba 在用户桌面上创建当前文件夹的快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12711040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a shortcut to current folder on user's desktop
提问by dan
I would like to automatically create a shortcut to the current's folder on the user's desktop. Some users I'm working with don't know how to create shortcuts or how to drag and drop a folder. I just want to create a file named "CLICK ME TO CREATE A SHORTCUT TO THIS FOLDER ON YOUR DESKTOP" that will work in any folder I want.
我想在用户桌面上自动创建当前文件夹的快捷方式。与我合作的一些用户不知道如何创建快捷方式或如何拖放文件夹。我只想创建一个名为“单击我在桌面上创建此文件夹的快捷方式”的文件,该文件可以在我想要的任何文件夹中使用。
For example, if I run C:\myRandomFolder\CLICK ME.whatever, I want it to create a shortcut to "C:\myRandomFolder\" named "myRandomFolder" on "D:\Documents and Settings\%username%\Desktop".
例如,如果我运行 C:\myRandomFolder\CLICK ME.whatever,我希望它在“D:\Documents and Settings\%username%\Desktop”上创建一个名为“myRandomFolder”的“C:\myRandomFolder\”快捷方式.
I'm wondering if I'm better using a batch file (.bat), VB Script (.vbs) or any other scripting language to do so. What would be the easiest and better way of doing it?
我想知道使用批处理文件 (.bat)、VB 脚本 (.vbs) 或任何其他脚本语言来这样做是否更好。什么是最简单和更好的方法?
回答by dan
The best way finally seems to be a VBS Script. Here is what I finally got working right:
最好的方法最后似乎是一个 VBS 脚本。这是我最终正确工作的内容:
Option Explicit
On Error Resume Next
Private WshShell
Private strDesktop
Private oShellLink
Private aSplit
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
aSplit = Split(WScript.ScriptFullName, "\")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & aSplit(Ubound(aSplit) - 1) & ".lnk")
oShellLink.TargetPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
oShellLink.WindowStyle = 1
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
oShellLink.Save
MsgBox "Shortcut to " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & " added yo your desktop!"
回答by 000
回答by tmoore82
Great code! Out of curiosity, since this works for the directory the script is currently in, do you have a way to get it to show up in every directory? Otherwise, it doesn't seem like there's much difference between learning this and learning to make a shortcut the native way. You would still have to drag and drop the script into the current folder, wouldn't you?
很棒的代码!出于好奇,由于这适用于脚本当前所在的目录,您是否有办法让它显示在每个目录中?否则,学习这个和学习以原生方式制作快捷方式之间似乎没有太大区别。您仍然需要将脚本拖放到当前文件夹中,不是吗?
While stumbling toward a solution, I got as far as letting users navigate to and select a particular file they need to link to. I don't know if you would have any use for that.
在努力寻找解决方案的过程中,我让用户导航到并选择他们需要链接到的特定文件。不知道你有没有用。
Dim diaSelectFile
Set diaSelectFile = Application.FileDialog(msoFileDialogFilePicker)
diaSelectFile.Show
strPickedFile = diaSelectFile.SelectedItems(1)
Set diaSelectFile = Nothing
Dim oWsh
Dim myshortcut
Dim oShortcut
Dim strSplitFileName
Dim strTarget
Dim nShortName
Set oWsh = CreateObject("WScript.Shell")
strSplitFileName = Split(strPickedFile, "\")
nShortName = UBound(strSplitFileName)
strTarget = strSplitFileName(nShortName)
myshortcut = "C:\users\%USERNAME%\Desktop\" & strTarget & " - Shortcut" & ".lnk"
Set oShortcut = oWsh.CreateShortcut(myshortcut)
With oShortcut
.TargetPath = strPickedFile
.Save
End With
Set oWsh = Nothing
Set oShortcut = Nothing
Again, though, this feels more complex than right-clicking and sending a shortcut to the desktop. Who are the users that need this? I know I've had austistic friends who struggle with what we might consider basic tasks on the computer. I'd definitely be interested to know if the script you came up with actually helps your clientele.
不过,这感觉比右键单击并将快捷方式发送到桌面要复杂得多。谁是需要这个的用户?我知道我有过自闭症的朋友,他们为我们认为是计算机上的基本任务而苦苦挣扎。我肯定很想知道你想出的剧本是否真的对你的客户有帮助。