2012-12-05 62 views
1

這是我第一次嘗試類似的東西,並有幾個問題。Outlook 2010宏保存附件並將其FTP到網絡服務器

我寫了下面的代碼是Outlook 2010宏。它將選定的電子郵件詳細信息保存在SQL數據庫中,將所有附件保存在本地,然後嘗試通過FTP將其上傳。

它看起來好像它正試圖上傳一個命令窗口打開,但然後再次快速關閉,但文件尚未上傳,我沒有得到任何錯誤信息。

任何人都可以看到我的代碼的任何問題?

Sub ExportToSql() 
Dim objConn As New ADODB.Connection 
Dim rsMsgs As New ADODB.Recordset 
Dim cmdMsgs As New ADODB.Command 
Dim strDBFile As String 
Dim sql As String 
Set objConn = New ADODB.Connection 

objConn.ConnectionString = "Provider=SQLOLEDB;data source=IP_ADDRESS;initial catalog=DB_NAME;User Id=USERNAME;Password=PASSWORD;" 
objConn.Open 

Set objExp = Application.ActiveExplorer 
If objExp.Selection.Count > 0 Then 
    If objExp.Selection(1).Class = Outlook.OlObjectClass.olMail Then 
     For Each objMsg In objExp.Selection 

      cmdMsgs.ActiveConnection = objConn 
      cmdMsgs.CommandText = "INSERT INTO [Email] ([Date], [Subject], [From], [To], [Body]) VALUES (?, ?, ?, ?, ?)" 
      cmdMsgs.Parameters.Append cmdMsgs.CreateParameter("param1", adDBTime, adParamInput, -1, objMsg.SentOn) 
      cmdMsgs.Parameters.Append cmdMsgs.CreateParameter("param2", adLongVarChar, adParamInput, 1000, objMsg.Subject) 
      cmdMsgs.Parameters.Append cmdMsgs.CreateParameter("param3", adVarChar, adParamInput, -1, objMsg.SenderEmailAddress) 
      cmdMsgs.Parameters.Append cmdMsgs.CreateParameter("param4", adVarChar, adParamInput, -1, objMsg.To) 
      cmdMsgs.Parameters.Append cmdMsgs.CreateParameter("param5", adLongVarChar, adParamInput, 50000, objMsg.Body) 
      cmdMsgs.Execute 

      For Each objAtt In objMsg.Attachments 

       'save file locally 
       Dim saveFolder As String 
       Dim filePath As String 
       saveFolder = "C:\Users\jw" 
       filePath = saveFolder & "\" & objAtt 
       objAtt.SaveAsFile filePath 
       Set objAtt = Nothing 



       'upload saved file 
       Dim vPath As String 
       Dim vFile As String 
       Dim vFTPServ As String 
       vPath = "C:\inetpub\wwwroot\WEBSITE\attachments" 
       vFTPServ = "IPADDRESS" 
       fNum = FreeFile() 
       Open filePath For Output As #fNum 
       Print #fNum, "USERNAME PASSWORD" 
       Print #fNum, "bin" ' bin or ascii file type to send 
       Print #fNum, "put " & filePath 
       Print #fNum, "close" ' close connection 
       Print #fNum, "quit" ' Quit ftp program 
       Close 
       Shell "ftp -n -i -g -s:" & vPath & " " & vFTPServ, vbNormalNoFocus 

      Next 
     Next 
    End If 
End If 

    objConn.Close 
    Set rsMsgs = Nothing 
    Set objConn = Nothing 

End Sub 

感謝您的幫助。 J.

+0

你如何檢查錯誤?你會得到一個保存在某個地方的命令窗口內容的副本嗎? – mellamokb

+0

將此命令添加到shell命令'&「> FTPOutput.txt」'的末尾,將FTP程序的輸出重定向到一個文本文件,以便您可以讀取可能返回的FTP錯誤(如果有的話)。 – Kratz

+0

感謝您的回答,無法讓輸出文件正常工作,嘗試了幾種不同的方法:Shell「ftp -n -i -g -s:」&vPath&「」&vFTPServ&「> C:\ Users \ jw \ FTPOutput.txt「,vbNormalNoFocus? – JBoom

回答

0

我覺得用戶名和密碼應該分在兩行。此外,使用「>>」替代「>」將FTP命令結果輸出到文本文件。

相關問題