Problem:
I had a working code for FTP Uploads sending Files to a Sever, but for some reason i get an Error when my Code executes ftpFolder.CopyHere lokalFolder.Items.Item((Dateiname)). The Error is Error 91 which in This case could mean one of those Errors on the official Microsoft Page. The Code was running fine until a few weeks ago.
I checked all the point out of the doc so far and tested my code but couldn't find the reason for the error.
My Question:
Could it be possible that witch this code gets Problems because i synchronised my current pc with OneDrive and the paths contain spaces? Or is it because of the Shell.Application Object is used wrong here? I can't seem to finde the Problem.
At this point i think i am just overlooking something.
Information about Environment the Code Runs:
The PC is Synced with Onedrive, so the String for the current Project path would look like something like this: C:...\Onedrive\CompanyName GmbH & Co. KG\Desktop
The Module:
Option Explicit
Public Enum LadeTyp
ltDownload
ltUpload
End Enum
#If 0 Then 'Schutz vor Überschreiben
Dim ltDownload, ltUpload
#End If
Public Sub FTPLoad(ByVal Laderichtung As LadeTyp, _
Server As String, _
Benutzer As String, _
passwort As String, _
LokalerOrdnerpfad As String, _
Optional Dateiname As String, _
Optional RemoteOrdnerpfad As String)
' Late Binding, kein Verweis auf 'Microsoft Shell Controls And Automation' erforderlich
Dim objShell As Object ' Shell32.Shell
Dim ftpFolder As Object ' Shell32.Folder
Dim lokalFolder As Object ' Shell32.Folder
Dim fi As Object ' Shell32.FolderItem
Dim strFTPVerbindung As String
' Slash am Pfadanfang entfernen
If Left(RemoteOrdnerpfad, 1) = "/" Then
RemoteOrdnerpfad = Mid(RemoteOrdnerpfad, 2)
End If
strFTPVerbindung = "ftp://" & Benutzer & ":" & passwort & "@" & Server & "/" & RemoteOrdnerpfad
Set objShell = CreateObject("Shell.Application")
' Argument in doppelte Klammern bei Late Binding
Set ftpFolder = objShell.Namespace((strFTPVerbindung))
Set lokalFolder = objShell.Namespace((LokalerOrdnerpfad))
' Ordner
If Dateiname = vbNullString Then
If Laderichtung = ltDownload Then
' Ganzen Ordner herunterladen
lokalFolder.CopyHere ftpFolder
ElseIf Laderichtung = ltUpload Then
' Ganzen Ordner hinaufladen
ftpFolder.CopyHere lokalFolder
End If
' Datei
Else
If Laderichtung = ltDownload Then
' Erste Datei im Ordner über ihren Index ansprechen,
' um die Dateigröße 0 bei der heruntergeladenen Datei zu vermeiden.
' Der Fehler wurde vermutlich inzwischen von Microsoft behoben.
' Set fi = ftpFolder.Items.Item(0)
' Datei herunterladen
' Argument in doppelte Klammern bei Late Binding
lokalFolder.CopyHere ftpFolder.Items.Item((Dateiname))
ElseIf Laderichtung = ltUpload Then
' Datei hinaufladen
' Argument in doppelte Klammern bei Late Binding
ftpFolder.CopyHere lokalFolder.Items.Item((Dateiname))
End If
End If
End Sub
My Code to call:
*generated a file and saved as test.csv in CurrentProject.Path*
...
strUser = "MyUser"
strPassword = "MyPass"
strServer = "ftp.myserver.com"
strRelRemotePfad = "/path/"
strPfadLokalerOrdner = CurrentProject.Path
strPfadLokalerDateiname = "test.csv"
Call FTPLoad(ltUpload, strServer, strUser, strPassword, _
strPfadLokalerOrdner, strPfadLokalerDateiname, strRelRemotePfad)