create a shortcut in VBA

  • Thread starter Thread starter Robert M via AccessMonster.com
  • Start date Start date
R

Robert M via AccessMonster.com

I am using an Access db to update an exisitng db or install new if necessary.
I would like to copy an exisitng shortcut to the users desktop. Can this be
done in code? If not, how do I create a shortcut in code?
 
Hi Robert,

The WSH (Windows Script Host) Shell object includes methods for
manipulating shortcuts. This sample code is from the Windows Script 5.6
Help (Script56.CHM; it's installed if you download the latest Windows
Script stuff from support.microsoft.com).


Set Shell = CreateObject("WScript.Shell")
DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\test.lnk")
link.Arguments = "1 2 3"
link.Description = "test shortcut"
link.HotKey = "CTRL+ALT+SHIFT+X"
link.IconLocation = "foo.exe,1"
link.TargetPath = "c:\blah\foo.exe"
link.WindowStyle = 3
link.WorkingDirectory = "c:\blah"
link.Save
 
Thanks, John. Now, I hate to be such a stupe but could you show me how to
invoke/launch a script from VBA code?
 
You don't need to use a separate VBScript. Just include the statements
to create and manipulate the WScript.Shell object in an ordinary VBA
procedure.

But read the documentation so you understand what you're doing.
 
John,
Thanks for info but I can't get any of the examples to compile. Am I missing
a reference, add-in or something else perhaps?

John said:
You don't need to use a separate VBScript. Just include the statements
to create and manipulate the WScript.Shell object in an ordinary VBA
procedure.

But read the documentation so you understand what you're doing.
Thanks, John. Now, I hate to be such a stupe but could you show me how to
invoke/launch a script from VBA code?
[quoted text clipped - 26 lines]
 
The example I posted doesn't need any references, add-ins or other fancy
stuff in order to compile. The only things it lacks are being enclosed
in a Begin Sub ... End Sub or Begin Function ... End Function, and
variable declarations. (All VBS variables are Variants, but I'd declare
Shell and Link As Object, and DesktopPath As String.)

Compiling and running are two different things: it won't run unless WSH
is installed on your computer.

John,
Thanks for info but I can't get any of the examples to compile. Am I missing
a reference, add-in or something else perhaps?

John said:
You don't need to use a separate VBScript. Just include the statements
to create and manipulate the WScript.Shell object in an ordinary VBA
procedure.

But read the documentation so you understand what you're doing.
Thanks, John. Now, I hate to be such a stupe but could you show me how to
invoke/launch a script from VBA code?
[quoted text clipped - 26 lines]
Please respond in the newgroup and not by email.
 
This may be a stupid question....but where do you put the script to create
the shortcut at? Do you call it from Access when the system opens or do you
place it somewhere else? Very interested in trying the code.

Thanks.
 
It depends on what you want to achieve. Normally you'd only need to run
it once, so it would probably go in an installation routine - which
might be in Access, or a VBScript, or a Visual Basic programme, or
whatever.

Or maybe you'd have code that runs when the database opens, performs a
check to try and discover whether it's the first time it's been opened
on that computer by that user, and if so asks the user if they want the
shortcut to be created.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top