More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 29-Jul-2014 01:37

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Login Password
Remember me
Register | Send my password
Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Send email attachment via FTP
Description: This code is triggered by a rule based on sender email address. It takes the email passed from the alert (which is selected based on specific sender address), and checks to see if it has attachment(s). For each attachment, it checks to see if its an XML file. If it is, it opens the file and searches for a special string. If it finds the string, it saves the file to local drive. It then attempts to FTP this file to a remote FTP server, using "WinINet" calls. Problem is it does everything fine until the FTP part. It does not seem to send the file and there's no feedback via my msgbox to tell me if it's failing. It does not even msgbox the string i've used as log, at the end of the sub.
Date: 14-Mar-2008  08:02
Code level: beginner
Code area: Basic Outlook
Posted by: David Oguara
This message is displayed as VB.NET
 
Option Explicit

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


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


Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean


Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer


Sub ProcessMyEmail(myMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim att As Attachments
Dim i As Integer
Dim fName As String
Dim fs, f
Dim lngINetConn, lngINet
Dim blnRC As Boolean
Dim UserName As String, Password As String, serverName As String
Dim localFile As String, hostFile As String
Dim myLog As String

Const myPath = "C:\"
Const myFileExt = "XML"
Const myAccCode = "MySpecialText"
strID = myMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
Set att = msg.Attachments
Const ASCII_TRANSFER = 1
Const BINARY_TRANSFER = 2
serverName = "myipaddress.com"
UserName = "myusername"
Password = "mypassword"
blnRC = False
hostFile = "orderABC.xml"

If att.Count > 0 Then
'Email has attachment(s)
'Debug.Print msg.Attachments.Count
For i = 1 To att.Count
fName = att(i).FileName
If UCase(Right(Trim(fName), 3)) = myFileExt Then
att(i).SaveAsFile (myPath & fName)
Set fs = CreateObject("Scripting.filesystemobject")
Set f = fs.OpenTextFile(myPath & fName, 1, 0)
If InStr(f, myAccCode, vbTextCompare) > 0 Then
'Found search text within attachment
localFile = myPath & fName
'close file and transfer to FTP
f.Close
Set f = Nothing
Set fs = Nothing
'Transfer to FTP
lngINet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
If lngINet > 0 Then
myLog = myLog & "Opened internet connection: " & Now() & vbCrLf 'Log connection
lngINetConn = InternetConnect(lngINet, serverName, 0, UserName, Password, 1, 0, 0)
If lngINetConn > 0 Then
myLog = myLog & "Connected to FTP host: " & Now() & vbCrLf ' Log FTP connection
blnRC = FtpPutFile(lngINetConn, localFile, hostFile, ASCII_TRANSFER, 0)
If (blnRC) Then
myLog = myLog & "Succcessful PUT file to FTP host: " & Now() & vbCrLf 'Log success PUT
Else
myLog = myLog & "Failed PUT file to FTP host: " & Now() & vbCrLf 'Log fail PUT
End If
InternetCloseHandle lngINetConn
End If
InternetCloseHandle lngINet
Else
'Initial Internet opened failed
MsgBox "Initial internet-open failed"
End If
End If
'Kill (myPath & fName) 'Delete saved attachment
End If
Next i
End If
MsgBox myLog
Set att = Nothing
Set msg = Nothing
Set olNS = Nothing
End Sub

All 1comments
Page [ 1  
  14-Mar-2008  09:36   
Discussion at http://www.outlookcode.com/threads.aspx?forumid=2&messageid=26099
Page [ 1