Почему метод fso.copyfile не находит путь? - PullRequest
0 голосов
/ 10 января 2019

Я просматриваю папки в Sharepoint и копирую все файлы .xlsx в папку на рабочем столе. Тем не менее, кажется, что существует проблема с поиском пути, что приводит к ошибке 76: путь не найден.

Я изучил этот веб-сайт, а также другие, чтобы найти решение, но не было ни одного, который бы работал для меня.

Это мой текущий код.

Dim path As String
Dim destination As String
Dim fso As Object
Dim obj_folder As Object
Dim obj_subfolder As Object
Dim file As Object

path = "\\mycompany.sharepoint.com\etc\etc"
destination = "C:\Users\adrian\Desktop\Practice\
Set fso = CreateObject("Scripting.filesystemobject")
Set obj_folder = fso.getfolder(path)

For Each obj_subfolder In obj_folder.subfolders
    For Each file In obj_subfolder.Files
        If InStr(1, file.Name, ".xlsx") Then
            Call fso.copyfile(file.path, destination & fso.getbasename(file) & ".xlsx")
        End If
    Next file
Next obj_subfolder

То, что я пробовал:

  1. Я включил справочник Microsoft Scripting Runtime.
  2. Я удалил & fso.getbasename (файл) & ".xlsx" из файла fso.copyfile. Takeda_DigitalTrialPlatform_RFI v2.xlsx

Ответы [ 2 ]

0 голосов
/ 11 января 2019
Аргумент

fso.GetBasenanme должен быть строкой, а не объектом file, что, вероятно, вызывает проблемы с копией. Поскольку вы уже знаете, что файл .xlsx, просто используйте исходное имя файла и используйте функцию fso.BuildPath.

Я бы изменил строку копирования следующим образом: fso.copyfile(file.path, fso.BuildPath(destination, file.name))

0 голосов
/ 10 января 2019

Попробуйте и измените ваши предпочтения

'*****************************************************
'* Find files in subfolders
'* Ver. 0.99
'*
Option Explicit
Const ROOTFOLDER = "X:"             'Change as desired
Const EXTENSION = "xlsx"            'Change as desired
Const FILES = "*." & EXTENSION

Dim g_FolderCount As Integer
Dim g_FileCount As Integer

Sub Test()                      'Test code. Replace with your actual code
    Dim Path As String

    g_FileCount = 0
    g_FolderCount = 0
    Path = ROOTFOLDER
    GetSubFolders Path
    Debug.Print "Number of folders: " & g_FolderCount & ". Number of files: " & g_FileCount
End Sub
'****************************************************************
'* Recursive sub to find path and files in subfolders
'*
Sub GetSubFolders(Path As String)
    Dim FSO As Object           'Late binding: Scripting.FileSystemObject
    Dim myFolder As Object      'Late binding: Folder
    Dim mySubFolder As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set myFolder = FSO.GetFolder(Path)
    If myFolder.SubFolders.Count <> 0 Then
        ProcessFiles Path                             'First branch (root)
        For Each mySubFolder In myFolder.SubFolders
            g_FolderCount = g_FolderCount + 1
            GetSubFolders mySubFolder.Path
        Next
    Else  'No more subfolders in Path, process files in current path
        ProcessFiles Path
    End If
End Sub
'*********************************************
'* Callback from GetSubFolders
'* Process files in the found folder
'*
Sub ProcessFiles(ByVal Path As String)
    Dim theFilePattern As String
    Dim theFile As String

    Path = Path & "\"
    theFilePattern = Path & FILES
    theFile = Dir(theFilePattern)
    While theFile <> ""    'Process each file here if needed
        g_FileCount = g_FileCount + 1
        Debug.Print Path & theFile
        theFile = Dir()    ' Next file if any
    Wend
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...