【问题标题】:Excel VBA check if shortcut xls file is pointing to valid fileExcel VBA检查快捷方式xls文件是否指向有效文件
【发布时间】:2015-03-14 11:31:53
【问题描述】:

我有一些代码循环遍历存储在网络驱动器文件夹中的每个文件。我循环浏览的文件是共享驱动器上其他各种文件夹中文件的快捷方式。

我的代码正在运行,但有一些快捷方式文件会抛出“缺少快捷方式”并要求我浏览文件系统。现在我不允许删除文件/快捷方式。有什么原因我可以访问快捷方式指向的位置,以便测试文件是否存在

我试过了:

Dim oFSO as object
Dim Folder as object
Dim Files as object
Dim file as Object

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = oFSO.GetFolder(thisWorkbook.Path)

For each file in Folders.files
  On Error GoTo fileErr:
  Workbooks.Open file.Path

  On Error GoTo 0
   Msgbox "File is opened"
   GoTo nextFile

fileErr:
  Msgbox Err.Description

nextFile:
Next file

任何帮助表示赞赏

【问题讨论】:

    标签: vba excel error-handling


    【解决方案1】:

    使用如下函数:

    Public Sub checkShortCut()
        MsgBox GetTargetPath("<Path>\<filename>.lnk")
        /*Your Code Here*/
    End Sub
    
    Function GetTargetPath(ByVal FileName As String)
    
        Dim Obj As Object
        Dim Shortcut As Object
    
        Set Obj = CreateObject("WScript.Shell")
        Set Shortcut = Obj.CreateShortcut(FileName)
        GetTargetPath = Shortcut.TargetPath
        Shortcut.Save
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2012-08-22
      • 2015-08-02
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多