2016-03-02 72 views
0

我正在使用以下功能使用FTP将文件上载到服务器。使用Access VBA的FTP在没有数据的服务器上创建文件

但文件大小为零。该文件不包含数据。无法弄清楚我在实现该功能时犯了什么错误。

Function FTP_Data() 
'========================================================================= 
'FTP from Microsoft Access 
'by Matthew V Carmichael 
'Creates FTP Batch File, FTP command file (txt) 
'=========================================================================' 
On Error GoTo Err_Trap 

    Dim pFile As Long 
    Dim strPath As String 
    Dim strFileName As String 
    Dim ftpServer As String 
    Dim strUserName As String 
    Dim strPassword As String 


    'Path and Name of file to FTP 
    strPath = "C:\Temp\" 
    strFileName = "C:\Temp\AccessDocumentation.pptx" 'Name of file to upload 
    'FTP Server Settings 
    ftpServer = "ftp.mydomain.com" 
    strUserName = "[email protected]" 
    strPassword = "****" 

    SetAttr strPath & "FTP_cmd.txt", vbNormal 


    'Create text file containing FTP commands 
    pFile = FreeFile 
    Open strPath & "FTP_cmd.txt" For Output As pFile 
    Print #pFile, "user" 
    Print #pFile, strUserName 
    Print #pFile, strPassword 
    Print #pFile, "Put " & strFileName 
    'Use the Put command to upload, use the Get command to download. 
    'Print #pFile, "Get " & "Your File Name" 
    Print #pFile, "quit" 
    Close pFile 

    'Create batch file to execute FTP 
    pFile = FreeFile 
    Open strPath & "FTP_Run.bat" For Output As pFile 
    Print #pFile, "ftp -n -s:" & strPath & "FTP_cmd.txt " & ftpServer 
    Print #pFile, "Pause" 
    Close pFile 

    SetAttr strPath & "FTP_cmd.txt", vbHidden 

    'Execute FTP command 
    Shell strPath & "FTP_Run.bat", 1 


Err_Trap_Exit: 
    Exit Function 

Err_Trap: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume Err_Trap_Exit 

End Function 

PS:新的VBA编程。

+0

它会手动运行吗?你确定它不应该读取:'strUserName =“user”'? – Gustav

+1

是的,第一步应该是创建一个工作的FTP_Run.bat + FTP_cmd.txt,第二步通过VBA创建这些(这应该是简单的部分)。 – Andre

+0

@Andre我试着手动运行它,发现同样的问题。因此创造了另一个相同的问题 - http://stackoverflow.com/questions/35743072 –

回答

0

我发现我的电脑本身存在问题。虽然找不到问题的确切原因。我检查防火墙,因为我关掉了防火墙,所以问题仍然存在。

上述代码在其他PC上正常工作。

我不删除这个问题,因为其他用户可能会发现这个问题,简单的解决方案是尝试在其他PC上运行代码。保持此Q开放的另一个原因是,这是为使用Access VBA搜索实施FTP的用户准备的代码。

相关问题