gpt4 book ai didi

VBA wscript.shell .close 给出错误

转载 作者:行者123 更新时间:2023-12-04 02:18:13 24 4
gpt4 key购买 nike

我在 Word2010 中使用以下代码(找到 here )来查找快捷方式的目标路径:

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

当我运行如图所示的代码(修改为使用我的快捷方式名称)时,出现以下错误:

Run-time error '438':

Object doesn't support this property or method

并且 .Close 行突出显示以进行调试。当我注释掉 .Close 时,脚本工作正常。

如果 shell 没有关闭,这会导致问题吗?我读过 .Close 对于 Wscript.Shell 不是必需的,但无法确认。

最佳答案

快捷方式对象没有 Close 方法,这就是您收到错误的原因。这link列出了 WScript.Shell 的基本操作。

如果您打算处置 shell 对象,最好的方法是

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

With objWshShell.CreateShortcut(Lnk)

.Save

Getlnkpath = .TargetPath

End With

Set objWshShell = Nothing

关于VBA wscript.shell .close 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com