Saving email attachments individually by changing their filename

GatesV

Member
Good day Gurus!

I'm looking at an issue where I could have multiple attachments in an email that have the same name. Knowing we can't save multiple files with the same name in the same folder, I'd like to loop through each individual attachment and choose the name under which it will be saved.

Is this possible with the current MS Outlook VBO? If not, what do you suggest?

Thank you very much for your time.
 

Sachin_Kharmale

Active Member
Hi @GatesV ,

If you want save attachment with different names then replace
Save Attachment Action Code of MS Outlook VBO with bellow Code.

Code:
    Dim OutlookMailBox As String = [MailBoxName]   
    Dim DestFolder As String = [Attachment_Local_Path]       
    If Not DestFolder.EndsWith("\") Then
        DestFolder = DestFolder & "\"
    End If
    
    
    Dim outlook as Object   
    Dim ns As Object     
    Dim Atmt As Object   
    Dim FileName AS String       
        
    Dim Item As Object

    outlook = CreateObject("Outlook.Application")
    ns = outlook.GetNamespace("MAPI")
    Item = ns.GetItemFromID(EntryID)

    Dim count as Integer = 1
    For Each Atmt In Item.Attachments                       
        If Add_DateTime_Stamp Then
            FileName = DestFolder & Format(Now, "dd-mmm-yyyy hh-mm-ss-fffff") & "_" & Atmt.FileName
        Else
            FileName = DestFolder & Atmt.FileName + count
            count=count+1
        End If
        Atmt.SaveAsFile(FileName)
    Next Atmt

Once you run above code it will save your Attachment with different names.
eg- if your email contains 3 Attachment then it will save your attachment Like

Attachment 1

Attachment 2
Attachment 3

I hope it will help you ..!

 

GatesV

Member
Thank you Sachin.

There's a problem with the code though as it doesn't account for the file extension. This means that if the filename is Attachment.pdf, it then becomes Attachment.pdf 1.

I've tried using the following code:
Code:
Dim app = CreateObject("Outlook.Application")
Dim _nameSpace = app.GetNameSpace("MAPI")

Dim item = _nameSpace.GetItemFromID(Entry_ID)
Dim extension As String
Dim counter As Integer = 1

If item Is Nothing Then Return

If Not Folder_Path.EndsWith("\") Then Folder_Path &="\"

For Each attachment As Object In item.Attachments
    If attachment.Type = 1 AndAlso attachment.FileName Like File_Pattern Then
        extension = Path.GetExtension(attachment.FileName)
        attachment.SaveAsFile(Folder_Path & Left(attachment.FileName, Len(attachment.FileName)-Len(extension)) & counter & extension)
        counter = counter + 1
    End If
Next

But I'm being returned the following error by Blue Prism when running the stage, even though the code checked fine in the object:
Internal : Could not execute code stage because exception thrown by code stage: Public member 'GetExtension' on type 'String' not found.

I'm not sure what's wrong with the code and how to fix it.

Thanks for your help.
 
Last edited:

Sachin_Kharmale

Active Member
Hi @GatesV ,

I understood what is the problem i have modified the code as per your requirement direct paste bellow code in
Save Attachments action of blue prism MS Outlook VBO.

Code:
    Dim OutlookMailBox As String = [MailBoxName]   
    Dim DestFolder As String = [Attachment_Local_Path]       
    If Not DestFolder.EndsWith("\") Then
        DestFolder = DestFolder & "\"
    End If
    
    
    Dim outlook as Object   
    Dim ns As Object     
    Dim Atmt As Object   
    Dim FileName AS String       
        
    Dim Item As Object

    outlook = CreateObject("Outlook.Application")
    ns = outlook.GetNamespace("MAPI")
    Item = ns.GetItemFromID(EntryID)
    Dim Count as Integer = 1
    For Each Atmt In Item.Attachments                       
        If Add_DateTime_Stamp Then
            FileName = DestFolder & Format(Now, "dd-mmm-yyyy hh-mm-ss-fffff") & "_" & Atmt.FileName
        Else
            Dim words As String() = Atmt.FileName.Split(".")
            
            FileName = DestFolder & words(0)  & Count & "." & words(1)
            Count = Count+1
        End If
        Atmt.SaveAsFile(FileName)
    Next Atmt
Also Refer bellow screen shots.
Screen Shot 1- Email Which Contains 3 Attachment
View attachment 1580361156184.png
Screen Shot 2 - File Names Renamed and save on Local Disc.

View attachment 1580361189252.png
I hope it will help you..!
 

GatesV

Member
That works real good! The only required adjustment is
Code:
FileName = DestFolder & Format(Now, "dd-MMM-yyyy hh-mm-ss-fffff") & "_" & Atmt.FileName
instead of
Code:
FileName = DestFolder & Format(Now, "dd-mmm-yyyy hh-mm-ss-fffff") & "_" & Atmt.FileName
Thank you. You've really helped me!
 

GatesV

Member
@Sachin_Kharmale
I see that you are using the following bit in your code:

Dim OutlookMailBox As String = [MailBoxName]

Is that supposed to let us choose a group mailbox as an input for where the emails see read from? I see you're not referring to OutlookMailBox anywhere else in the code.

Thanks
 
Top