Open .lnk shortcut files using EXCEL VBA

craig159753

New Member
Joined
Apr 21, 2015
Messages
24
Hi all,

I am trying to open a SAS.EXE application through a .lnk file (i.e. a shortcut). The code I have so far is as follows

First bit declares a new function:

Code:
Public Declare Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long

This bit is my VBA call to open said .lnk file.

Code:
Sub mymacro()
    
    ShellExecute 0, "OPEN", "C:\Docs\myshortcut.lnk", "", "", 1
    
End Sub

The code does not fail, when i run it the mouse icon loads for one second then nothing, any ideas?

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Use "Shell" method of VBA library:
Code:
Shell "C:\myprog.exe", vbNormalFocus
 
Upvote 0
I did originally try this and I got the following error,

SYNTAX ERROR

I shoukd note that my shortcut file has a space in it, not sure if this matters

Code:
Shell ("[COLOR=#333333]C:\Docs\my shortcut.lnk[/COLOR]", vbNormalFocus)
 
Upvote 0
I tried your code. It works well. The only difference is that I had to add "PtrSafe" keyword as I have 64-bit Office.
 
Upvote 0
As you see, I just added PtrSafe keyword.
Code:
Public Declare PtrSafe Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
As Long


Sub FFF()
    ShellExecute 0, "OPEN", "C:\Users\User\Desktop\MyLink.lnk", "", "", 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,222
Messages
6,129,586
Members
449,520
Latest member
TBFrieds

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top