Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 连接到FTPS服务器的Excel宏代码,即通过SSL而不是FTP服务器的FTP_Vba_Excel_Ssl_Ftp - Fatal编程技术网

Vba 连接到FTPS服务器的Excel宏代码,即通过SSL而不是FTP服务器的FTP

Vba 连接到FTPS服务器的Excel宏代码,即通过SSL而不是FTP服务器的FTP,vba,excel,ssl,ftp,Vba,Excel,Ssl,Ftp,问题描述: 无法连接到FTPS服务器,这是通过SSL或FTPS的专用服务器FTP。它是与HTTPS相同的安全连接。我正在使用的代码已成功连接到公共服务器。但似乎为了连接到安全的FTPS服务器,我需要使用某种SSL加密。我一点也不知道,我是java的家伙,但我被要求解决这个问题,学习新东西总是很有趣的,这次是VBA代码。请帮助我的VBA专家 有人能告诉我,为了连接到我的FTPS服务器,我应该修改或添加什么代码,代码在哪里。请再次注意,我可以连接到FTP服务器,但不能连接到FTPS 下面是我目前使用

问题描述: 无法连接到FTPS服务器,这是通过SSL或FTPS的专用服务器FTP。它是与HTTPS相同的安全连接。我正在使用的代码已成功连接到公共服务器。但似乎为了连接到安全的FTPS服务器,我需要使用某种SSL加密。我一点也不知道,我是java的家伙,但我被要求解决这个问题,学习新东西总是很有趣的,这次是VBA代码。请帮助我的VBA专家

有人能告诉我,为了连接到我的FTPS服务器,我应该修改或添加什么代码,代码在哪里。请再次注意,我可以连接到FTP服务器,但不能连接到FTPS

下面是我目前使用的代码。这将是一个很大的帮助谢谢你

'API code

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Const MAX_PATH = 260
Private Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type


 Private Declare Function InternetOpen _
   Lib "wininet.dll" _
     Alias "InternetOpenA" _
       (ByVal sAgent As String, _
        ByVal lAccessType As Long, _
        ByVal sProxyName As String, _
        ByVal sProxyBypass As String, _
        ByVal lFlags As Long) As Long


        'Connect to the network
 Private Declare Function InternetConnect _
   Lib "wininet.dll" _
     Alias "InternetConnectA" _
       (ByVal hInternetSession As Long, _
        ByVal sServerName As String, _
        ByVal nServerPort As Integer, _
        ByVal sUsername As String, _
        ByVal sPassword As String, _
        ByVal lService As Long, _
        ByVal lFlags As Long, _
        ByVal lContext As Long) As Long

        'Get a file using FTP
 Private Declare Function FtpGetFile _
   Lib "wininet.dll" _
     Alias "FtpGetFileA" _
       (ByVal hFtpSession As Long, _
        ByVal lpszremoteDir As String, _
        ByVal lpszNewFile As String, _
        ByVal fFailIfExists As Boolean, _
        ByVal dwFlagsAndAttributes As Long, _
        ByVal dwFlags As Long, _
        ByVal dwContext As Long) As Boolean


        'Close the Internet object
 Private Declare Function InternetCloseHandle _
   Lib "wininet.dll" _
     (ByVal hInet As Long) As Integer


        '
    Private Declare Function FtpFindFirstFile _
    Lib "wininet.dll" _
        Alias "FtpFindFirstFileA" _
        (ByVal hFtpSession As Long, _
        ByVal lpszSearchFile As String, _
        lpFindFileData As WIN32_FIND_DATA, _
        ByVal dwFlags As Long, _
        ByVal dwContent As Long) As Long

        Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long

Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
(ByVal hConnect As Long, ByVal lpszDirectory As String) As Long



'***************
'downloadFile method downloads files from a specified server through FTP
'This method downloads files on only first level of specified directory on the server
'
'
'
'
'***************

'***************
'To do
'
'1. if localDir does not include "\", it does not work - fixed
'2. if folders exist on the remote server, it will not download
'
'
'***************

'download files from a specified server

Public Function downloadFiles(ServerName As String, UserName As String, Password As String, remoteDir As String, localDir As String, logFile As String) As Variant()

Dim INet As Long
Dim INetConn As Long
Dim RetVal As Long
Dim Success As Long
Dim hFile As Long
Dim w32FindData As WIN32_FIND_DATA
Dim StrFile As String
Dim fileList() As String
Dim cnt As Long
Dim gcnt As Long
Dim i As Integer
Dim curDir As Long
Dim result(1) As Variant


cnt = -1
gcnt = 0
RetVal = False


Rem confirm local dir has \ at the end

If Not Right(localDir, 1) = "\" Then
    localDir = localDir + "\"
End If

'Test Code need to remove as the username and password are hardcoded


INet = InternetOpen("MYFTP Control", 1&, vbNullString, vbNullString, 0&)
    If INet > 0 Then
        INetConn = InternetConnect(INet, ServerName, 0&, UserName, Password, 1&, 0&, 0&)
        If INetConn > 0 Then
            file.log "==== Connected to " & ServerName & "===", logFile


        curDir = FtpSetCurrentDirectory(INetConn, remoteDir)
        If (curDir <> 0) Then
            file.log "current remote dir: " & remoteDir, logFile
        End If

            ''''''''''''''''''''
            ''Create a list of files to download
            ''''''''''''''''''''


            'get file list
            hFile = FtpFindFirstFile(INetConn, remoteDir, w32FindData, INTERNET_FLAG_RELOAD, 0&)


            'create a list of files on the remote server
        If hFile = 0 Then
            file.log "cannot get a list of files", logFile
        Else

            Do

                StrFile = Left(w32FindData.cFileName, InStr(w32FindData.cFileName, vbNullChar) - 1)
                StrFile = Mid(StrFile, InStrRev(StrFile, " ") + 1)

                'if the path is directory, skip this
                If ((w32FindData.dwFileAttributes And &H10) <> &H10) Then
                    'strFile = strFile & "/"


                cnt = cnt + 1

                ReDim Preserve fileList(cnt)
                fileList(cnt) = StrFile
                Debug.Print StrFile 'Debug

                End If 'end of skiping dir condition

            Loop Until InternetFindNextFile(hFile, w32FindData) = 0


            ''''''''''''''''''''
            ''Download files on the list
            ''''''''''''''''''''
            For i = 0 To cnt

                'set local file
                StrFile = localDir & fileList(i)

                    'download a file
                    Success = FtpGetFile(INetConn, fileList(i), StrFile, False, FILE_ATTRIBUTE_NORMAL, BINARY_TRANSFER, 0&)

                    If Success > 0 Then
                    file.log fileList(i) & " is downloaded", logFile
                    gcnt = gcnt + 1

                    Else
                    file.log fileList(i) & " is Not downloaded", logFile

                    End If

            Next


        End If

        RetVal = InternetCloseHandle(INet)


        Else


        'cannot connet to the server error message
        file.log "Client cannnoot connet to " & ServerName, logFile
        RetVal = InternetCloseHandle(INet)

        End If

    End If




result(0) = cnt + 1
result(1) = gcnt


file.log ServerName & " - " & "Downloaded files: " & CStr(result(1)) & " out of " & CStr(result(0)), logFile

        If RetVal > 0 Then
            file.log "===Connection is closed===", logFile
        Else
            file.log "===Connection is not closed correctly===", logFile
        End If


downloadFiles = result


End Function

Private Function log(warnLevel As String, info As String, fileName As String)




End Function

Private Function msg(info As String)

    MsgBox info

End Function
'API代码
私有类型文件时间
dwLowDateTime尽可能长
dwHighDateTime尽可能长
端型
私有常量最大路径=260
私有类型WIN32\u查找\u数据
dwFileAttributes尽可能长
ftCreationTime作为文件时间
ftLastAccessTime作为文件时间
ftLastWriteTime作为文件时间
nFileSizeHigh尽可能长
只要
DW保留的长度为
D保留的时间尽可能长
cFileName作为字符串*MAX\u路径
cAlternate作为字符串*14
端型
私有声明函数InternetOpen_
库“wininet.dll”_
别名“InternetOpenA”_
(ByVal sAgent作为字符串_
ByVal lAccessType,只要_
ByVal sProxyName作为字符串_
ByVal sProxyBypass作为字符串_
ByVal lFlags(长)一样长
'连接到网络
专用声明函数InternetConnect_
库“wininet.dll”_
别名“InternetConnectA”_
(ByVal hInternetSession,只要_
ByVal sServerName作为字符串_
ByVal N服务器端口为整数_
ByVal sUsername作为字符串_
ByVal sPassword作为字符串_
ByVal L服务,只要_
拜瓦尔和我一样长_
ByVal lContext As Long)As Long
'使用FTP获取文件
私有声明函数FtpGetFile_
库“wininet.dll”_
别名“FtpGetFileA”_
(ByVal hFtpSession,只要_
ByVal lpszremoteDir作为字符串_
ByVal lpszNewFile作为字符串_
ByVal ffailife以布尔形式存在_
ByVal dwFlagsAndAttributes的长度_
拜瓦尔:只要_
ByVal dwContext(长度)为布尔值
'关闭Internet对象
私有声明函数InternetCloseHandle_
库“wininet.dll”_
(ByVal hInet等长)作为整数
'
私有声明函数FtpFindFirstFile_
库“wininet.dll”_
别名“FtpFindFirstFileA”_
(ByVal hFtpSession,只要_
ByVal LPSZSSearchFile作为字符串_
lpFindFileData作为WIN32_FIND_数据_
拜瓦尔:只要_
ByVal dwContent As Long)As Long
私有声明函数InternetFindNextFile Lib“wininet.dll”别名“InternetFindNextFileA”_
(ByVal hFind为Long,lpvFindData为WIN32_FIND_DATA)为Long
私有声明函数FtpSetCurrentDirectory Lib“wininet.dll”别名“FtpSetCurrentDirectoryA”_
(ByVal hConnect作为Long,ByVal lpsz directory作为String)作为Long
'***************
'downloadFile方法通过FTP从指定服务器下载文件
'此方法仅下载服务器上指定目录的第一级上的文件
'
'
'
'
'***************
'***************
”“怎么办
'
'1.如果localDir不包含“\”,则它不起作用-已修复
“2.如果远程服务器上存在文件夹,则不会下载
'
'
'***************
'从指定服务器下载文件
公共函数下载文件(ServerName作为字符串,UserName作为字符串,Password作为字符串,remoteDir作为字符串,localDir作为字符串,logFile作为字符串)作为变量()
暗淡如长
昏暗的康涅狄格河
暗淡的后退
只要成功
暗文件一样长
将W32查找数据作为WIN32查找数据
作为字符串的Dim StrFile
Dim fileList()作为字符串
暗淡的碳纳米管
将gcnt设置为长
作为整数的Dim i
长得一样暗
变光结果(1)作为变量
cnt=-1
gcnt=0
RetVal=False
Rem确认本地目录在结尾处\
如果不正确(localDir,1)=“\”则
localDir=localDir+“\”
如果结束
'测试代码需要删除,因为用户名和密码是硬编码的
INet=InternetOpen(“MYFTP控件”,1&,vbNullString,vbNullString,0&)
如果INet>0,则
INetConn=InternetConnect(INet、服务器名、0&、用户名、密码、1&、0&、0&)
如果INetConn>0,则
file.log“===已连接到”&ServerName&“==”,日志文件
curDir=FtpSetCurrentDirectory(INetConn,remoteDir)
如果(curDir 0)那么
file.log“当前远程目录:”&远程目录,日志文件
如果结束
''''''''''''''''''''
''创建要下载的文件列表
''''''''''''''''''''
'获取文件列表
hFile=FtpFindFirstFile(INetConn、remoteDir、w32FindData、INTERNET\u标志\u重载、0&)
'在远程服务器上创建文件列表
如果hFile=0,则
file.log“无法获取文件列表”,logFile
其他的
做
StrFile=Left(w32FindData.cFileName,InStr(w32FindData.cFileName,vbNullChar)-1)
StrFile=Mid(StrFile,InStrRev(StrFile,“”)+1)
'如果路径是目录,请跳过此操作
如果((w32FindData.dwFileAttributes和&H10)&H10),则
'strFile=strFile&“/”
cnt=cnt+1
ReDim保留文件列表(cnt)
文件列表(cnt)=StrFile
调试。打印StrFile“调试”
如果“跳过方向条件结束,则结束”
循环直到InternetFindNextFile(hFile,w32FindData)=0
''''''''''''''''''''
''下载列表上的文件
''''''''''''''''''''
对于i=0到cnt
'设置本地文件
StrFile=localDir&fileList(i)
'下载文件