0

I used an Outlook Rule function to determine all the emails that I receive from this "X" email address and the Rule is set-up in such a way that it will run a VBA script when the rules scriteria is found (see below VBA script).

The problem that I am having with this VBA script is that only the last email attachment gets saved from "X" user. Meaning the Rule will be applied to all the emails, however the VBA code - which is intended to save the attachments of each email - only saves the last email's attachment.

'''
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String

sSaveFolder = "L:\my file path I save the PDFs to\"

For Each oAttachment In MItem.Attachments
    oAttachment.SaveAsFile sSaveFolder & "\Signature -" & i & ".pdf" 'oAttachment.DisplayName
    Set oAttachment = Nothing
Next
End Sub
'''

I even tried renaming the PDFs so that VBA does not have an issue with re-saving the same name on top of the previous PDF, but this did not help - issue still persists and only last email attachment gets saved. Please help :)

'''
    Public Sub saveAttachtoDisk2(itm As Outlook.MailItem)
    Dim objtest As Outlook.Application
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim x As Integer
    saveFolder = "L:\L:my file path I save the PDFs to\"
    
    
    x = 1
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\Signature - " & x '& objAtt.DisplayName
    Set objAtt = Nothing
    x = x + 1
    Next 
    End Sub
'''
FabioAuto
  • 31
  • 2
  • Applying an incrementing number is feasible if the value is saved and retrieved when necessary. https://stackoverflow.com/questions/20638061/generate-automatic-ids – niton Jul 19 '22 at 20:53

1 Answers1

0
dim i as long
i = i+1 in your loop
dumples
  • 91
  • 3
  • I think the OP used `x`. It would have failed since x is assigned the value 1 for each mail, never changing to 2. In your suggestion i would reinitialize to 0 on each run. – niton Jul 19 '22 at 20:40