0

I wanted to save an attached excel file in outlook to a ftp space with VBA.

I have mapped ftp folder in Windows 10 and I can reach it. But macro cannot save attachment in that folder. The code is

Public Sub CheckEmail_BlueRecruit()

    Dim outlookApp As Outlook.Application
    Dim outlookNamespace As Outlook.NameSpace
    Dim outlookFolder As Outlook.MAPIFolder
    Dim filterKeywords As String
    Dim filter As String
    Set outlookApp = New Outlook.Application
    Set outlookNamespace = Outlook.GetNamespace("MAPI")
    Set outlookFolder = outlookNamespace.GetDefaultFolder(olFolderInbox)

    filterKeywords = "I: Reservations"
    filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%" & filterKeywords & " %'"

    LoopFolders outlookFolder, filter
End Sub

Private Sub LoopFolders(ByVal outlookFolder As Outlook.MAPIFolder, ByVal filter As String)

    Dim outlookSubFolder As Outlook.MAPIFolder
    Dim outlookMail As Outlook.MailItem

    ProcessFolder outlookFolder, filter

    If outlookFolder.Folders.Count > 0 Then
        For Each outlookSubFolder In outlookFolder.Folders
            LoopFolders outlookSubFolder, filter
        Next
    End If

End Sub

Private Sub ProcessFolder(ByVal outlookFolder As Outlook.MAPIFolder, ByVal filter As String)

    Dim outlookItems As Outlook.Items
    Dim outlookMail As Outlook.MailItem

    Set outlookItems = outlookFolder.Items.Restrict(filter)

    If Not outlookItems Is Nothing Then

        For Each outlookMail In outlookItems

            If outlookMail.Attachments.Count <> 0 Then
                For i = outlookMail.Attachments.Count To 1 Step -1
                'Debug.Print outlookMail.Subject
                    strFile = outlookMail.Attachments.Item(i).FileName
                    Debug.Print strFile
                    strFolderpath = "ftp://myserverexample.it/public_html/check/import/drive/"
                    outlookMail.Attachments.Item(i).SaveAsFile strFolderpath & strFile
                Next i
            End If

        Next outlookMail

    End If

End Sub

Manually I can navigate the FTP folder as a normal Window folder, but macro cannot save in that position, Why? Thx a lot

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Marco Bozzola
  • 179
  • 1
  • 3
  • 17
  • 2
    You need to `SaveAsFile` to the hard disk (even if it is just temporary) and then see [Upload file via FTP from Excel VBA](https://stackoverflow.com/questions/7737691/upload-file-via-ftp-from-excel-vba) (and do the same in Outlook which is the same as in Excel). – Pᴇʜ Jun 18 '21 at 12:57

1 Answers1

0

The Attachment.SaveAsFile method takes a string as a parameter which stands for the location at which to save the attachment. The string should be represented by a local file path. After saving the file on the hard drive locally you can upload it to any ftp server programmatically.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45