2016-03-02 70 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的用戶準備的代碼。

相關問題