Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute shortcuts using VB 6.0? 1

Status
Not open for further replies.

ifa

MIS
Apr 29, 1999
9
ES
Hi everybody.

I am tryng to execute a Windows shortcut (.lnk) calling it from VB6, but it fails. I am using a call similar to this:

RetVal = Shell("C:\MailBell.lnk", 1)


Is there anyway to execute a shortcut in VB6?

TIA,
Aki
 
Option Explicit

Const SW_SHOWNORMAL = 1

Private 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

ShellExecute hwnd, "open", "c:\windows\desktop\test.lnk", vbNullString, vbNullString, SW_SHOWNORMAL
 
Thanks LPlates.

I have try this code to open bookmarks, programs, images, ... and it works fine but when I try to open a shortcut it fails (Nothing appears).

----------------------------
Option Explicit
Private 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

Const SW_SHOWNORMAL = 1

Private Sub Form_Load()
Dim i As Long
i = ShellExecute(hwnd, "open", "D:\shortcut.lnk", "", "", SW_SHOWNORMAL)
End
End Sub
------------------------

I am using VB6, Windows XP.

Any idea about this?

TIA,
Aki
 
The shortcut name is for example...

Program shortcut points too: c:\test.exe

Shortcut put in ... c:\AnyDir

Shortcut path to execute would be...

c:\AnyDir\test.exe.lnk

Any help?
 
Hi LPlates.

I have placed a copy of Notepad.exe on "D:\" (D:\NOTEPAD.EXE)
I have created a shortcut to that copy on "D:\" (D:\Shortcut.lnk)

If I double click "D:\Shortcut.lnk" it opens the Notepad without problem, but if I execute the above code it does nothing.

TIA,
Aki
 
Um...I think I can see what is happening here...are you renaming the shortcuts? And, if so, are you manually adding ".lnk" to the name? I ask because shortcuts already have .lnk as their file extension, but this is hidden from you in the GUI (even if you have chosen not to hide file extensions via Folder Options).

In other words, when you rename a shortcut to "shortcut.lnk" the real filename is "shortcut.lnk.lnk" (and it is this real filename that needs to be passed to the ShellExec function
 
Thanks Strongm.

But, I haven't renamed the Shortcut. The realname is shortcut.lnk. I can see it in a DOS box.
 
> I haven't renamed the Shortcut

Ok, if you say so. I'd still argue that the symptoms your are experiencing and explaining are 100% compliant with the filename being wrong.

How are you creating the shortcut? Normally, if I was creating (via the GUI) a shortcut to Notepad, that shortcut would be (in the GUI) "Shortcut to Notepad", and not "Shortcut". So, if you are not renaming anything, how are you getting shortcuts called "Shortcut"?
 
OK strongm.

I have renamed the shortcut, but I was meaning I haven't renamed the extension or I haven't added an extra extension. After all, if I see a file called shortcut.lnk in a DOS box. That's the filename I ought to use calling to ShellExecute.

Anyway, Thank you.

Aki
 
Stick a

MsgBox Dir("D:\shortcut.lnk")

just before the ShellExecute line in your project. My money is on an error being raised...
 
I am sorry. You lose!

A MsgBox Dialog appears with the message 'shortcut.lnk'.

Any other suggestion?

TIA,
Aki
 
Strongm? Wrong?

Run the test again - you typed it in wrong...

mmilan
 
Is there anything else you are not telling us?

I ask because:
a) You said you hadnt renamed the shortcut, but then agreed you had done but didn't think it important enough to originally admit
b) the technique outlined by LPlates is one I use regularly myself (along with two other techniques) and it works fine here on W98, W2K and XP. SO there has got to be something else going on...
 
Hi.

I am not trying to confuse you, or tryng you waste your valuable time. I am asking help because I have a problem. Apparently the problem (or the solution) is very simple but I have, at this moment, a piece of code that doesn't work as is expected.

I didn't want to hide that I had renamed the shortcut. Simply, I knew that the GUI wasn't showing the real name of the file (because of that I said: 'I can see it in a DOS box').

I have created a new shortcut to NOTEPAD.EXE. I have copied the filename showed in a DOS box ('Acceso directo a NOTEPAD.EXE.lnk') and pasted in my project, replacing the 'Shortcut.lnk' used before. And I have the same result. A Msgbox Dialog showing 'Acceso directo a NOTEPAD.EXE.lnk' and nothing else.

PS: My Windows XP is the Spanish version.

Thanks,
Aki
 
OK.

Just try this code instead of ShellExecute and see what happens...
[tt]
Shell "start ""D:\Shortcut.lnk""", vbHide[/tt]

I can use Shell "start ... " on my machine to execute documents including shortcuts like ShellExecute.
 
>I am asking help because I have a problem

S'OK, I appreciate that. I was just trying to get more info, since the code as provided by LPlates should work, and does work here on several machines and with several different Windows OS. The conclusion has to be that there is something different about your setup, and we need to try and find out what it is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top