vba 运行时错误 76 找不到路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19511625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:00:03  来源:igfitidea点击:

Runtime Error 76 Path not found

excelvbaruntime-error

提问by Chris

I have this Error Message, i'm completely lost...

我有这个错误信息,我完全迷路了......

I think I checked everything that could be wrong, maybe one of you guys can see a mistake or something. My brain is now completely blocked.

我想我检查了所有可能出错的地方,也许你们中的一个人可以看到错误或其他什么。我的大脑现在完全被阻塞了。

Thanks in advance

提前致谢

Option Explicit

Public newestFile As Object

Sub Scan_Click()
    Dim path As String
    Dim row As Integer: row = 2
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("ETA File Server")

    With ws
        Do
            If .Cells(row, 1).Value = "" Then Exit Do

            path = .Cells(row, 1).Value

            Application.StatusBar = "Processing folder " & path
            DoEvents

            If .Cells(row, 1).Value <> "Root" Then
                Call getNewestFile(path)

                .Cells(row, 9).Value = newestFile.DateLastModified
                .Cells(row, 10).Value = newestFile.Name

                Set newestFile = Nothing
                row = row + 1
            Else
                row = row + 1
            End If
        Loop
    End With

    Application.StatusBar = "Done"
End Sub

Private Sub getNewestFile(folderpath As String)
    Dim objFSO As Object, objFolder As Object, objFile As Object

    'get the filesystem object from the system
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(folderpath)

    'go through the subfolder and call itself
    For Each objFile In objFolder.SubFolders
        Call getNewestFile(objFile.path)
        DoEvents
    Next


    For Each objFile In objFolder.Files
        If newestFile Is Nothing Then
            Set newestFile = objFile
        ElseIf objFile.DateLastModified > newestFile.DateLastModified Then
            Set newestFile = objFile
        End If
    Next
End Sub

采纳答案by Chris

Allright i found an answer! Windows can only handle paths under 255characters...

好吧,我找到了答案!Windows 只能处理 255 个字符以下的路径...

so all you have to do is add \?\ before a path, for example \\?\c:\userson server adressen you have to add \?\unc --> \\?\unc\servername\path

所以你所要做的就是在路径前添加 \?\,例如\\?\c:\users在服务器地址上你必须添加 \?\unc -->\\?\unc\servername\path

hope that helps you out!

希望能帮到你!

回答by Nishanth Pinto

I get this error when the file I am trying to reach is in SharePoint. As a workaround what I do is I will open that link in Explorer view (SharePoint link - Library - Connect & Export - Open with Explorer). Once I have the SP in explorer view it functions smoothly. To resolve this we have to map that SP link into a drive and call the drive address instead of SP link. Link for that - Get the content of a sharepoint folder with Excel VBA

当我尝试访问的文件位于 SharePoint 中时,我收到此错误。作为一种解决方法,我将在资源管理器视图中打开该链接(SharePoint 链接 - 库 - 连接和导出 - 使用资源管理器打开)。一旦我在资源管理器视图中拥有 SP,它就会顺利运行。为了解决这个问题,我们必须将该 SP 链接映射到驱动器并调用驱动器地址而不是 SP 链接。链接 -使用 Excel VBA 获取共享点文件夹的内容

回答by user3565022

It could be caused by the long file name due to extensive folder & subfolder of a file to be copied.

这可能是由于要复制的文件的文件夹和子文件夹很大而导致文件名过长。

Try reduce the length of the name of all the folders / subfolders before you copy.

在复制之前尝试减少所有文件夹/子文件夹的名称长度。

It solve my problem, hope solving yours too.

它解决了我的问题,希望也能解决你的问题。

Regards,

问候,