Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA wscript.shell .close giving error

Tags:

shell

ms-word

vba

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.

like image 613
Dan W Avatar asked Oct 22 '25 02:10

Dan W


1 Answers

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
like image 115
Shalvin Abraham Avatar answered Oct 23 '25 17:10

Shalvin Abraham