Dan W
Dan W

Reputation: 131

VBA wscript.shell .close giving error

I am using the following code (as found here) in Word2010 to find the target path of a shortcut:

Function Getlnkpath(ByVal Lnk As String) 
On Error Resume Next 
With CreateObject("Wscript.Shell").CreateShortcut(Lnk) 
    Getlnkpath = .TargetPath 
    .Close 
End With 
End Function 

Sub GetLinkPath() 
    MsgBox Getlnkpath("yourshortcutnamehere") 
End Sub 

When I run the code as shown (modified to use my shortcut name) I get the following error:

Run-time error '438':

Object doesn't support this property or method

and the .Close line is highlighted for debug. When I comment out .Close the script works fine.

Does this cause problems if the shell doesn't close? I've read that .Close isn't necessary for Wscript.Shell but can't confirm that.

Upvotes: 4

Views: 6222

Answers (2)

Ritesh  Karwa
Ritesh Karwa

Reputation: 2254

Posting this answer for anyone who is still stuck on trying to close the WScript.Shell Object after creating it and not able to find a solution. My Vb Script :

        Dim wsh As Object
        Set wsh = CreateObject("WScript.Shell", vbNothing)
        wsh.Run "cmd.exe /C pause"
        wsh.Run "taskkill /F /IM cmd.exe"

Upvotes: 3

Shalvin Abraham
Shalvin Abraham

Reputation: 458

There is no Close method to the shortcut object and that is why you are getting the error. This link lists basic operations of WScript.Shell.

If you are intending to dispose the shell object, the best way to do would be

Set objWshShell = WScript.CreateObject("WScript.Shell")

With objWshShell.CreateShortcut(Lnk)

.Save

Getlnkpath = .TargetPath

End With

Set objWshShell = Nothing

Upvotes: 3

Related Questions