save attachment


i want to save an email attachment to a specific folder, and I want to include part of the name of the sender in the saved file.

For example, if the name of the sender is ABC12345678, I want to save the attachment and name it '12345678' and append to it also the attachment name. Note that I want to discard the first three letters of the name of the sender.

So for instance the name of the saved file would be 12345678_NameOfAttachment.doc

Thanks in advance for your help
Who is Participating?

Improve company productivity with a Business Account.Sign Up

stefriConnect With a Mentor Commented:
Mid to extract part of a string
Prototype is
Mid(string, start, lenghtOfString)
You want to extract part of the string starting at character 4 so start = 4
The length to be returned is FullLength - start +1, therfore
Mid(yourSenderName, 4, len(yourSenderName) - 4+ 1)

If you switch to VBA editor in OL, Xls or word, start typing a function, the prototype will be displayed
Selecting a VBA function then F1 brings help
You can alse: View/Object Reference then browse and search for functions and how to use them


Good luck with that...Outlook scripting is somewhat different from VB/VBA...
If you want to pursue it - here's some sites to get you started:

I'd suggest getting one of the Outlook Programming books.
Anything by Sue Mosher is top notch - she's really terrific with OL.

Hope that helps.
chspitAuthor Commented:
Can't I do it with VBA, as a macro? I know how to save the attachment to a specific folder using VBA. What I don't know is how to name the saved attachment with the name of the sender using VBA.
Is this possible. If so, how?
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I don't believe so...another expert may come along and shed some insight - but Outlook VBA is, imho, very cumbersome compared to VBA...
I believe this one can be solved by stefri.  He wrote the following script to be included in LINK EM UP ON OUTLOOK due out in June.

Sub saverep()
    Dim oApp As Application
    Dim oNS As NameSpace
    Dim oMessage As Object
    Dim oAttachments As Outlook.Attachments
    Dim NbrMsgs
    Set oApp = New Outlook.Application
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    NbrMsgs = 0
    For Each oMessage In oFolder.Items
        With oMessage
            If .SenderName = "TheBoss" Then
                NbrMsgs = NbrMsgs + 1
                oMessage.Attachments.Item(1).SaveAsFile "C:\reports\in\" _ & NbrMsgs & "report.txt"
            End If
        End With
End Sub

Change SaveAsFile to use oMessage.Sendername and it should work well.  I will also send this to stefri to get a more experienced opinion.
Slink9 is correct
With SaveAs method, just name it as:
 oMessage.Attachments.Item(1).SaveAsFile "C:\reports\in\" & _
     Mid(oMessage.Sendername , 4, Len(oMessage.Sendername ) - 3)

chspitAuthor Commented:
What is >>>Mid(oMessage.Sendername , 4, Len(oMessage.Sendername ) - 3)

Can you please guide me to some documentation. Thanks
In your example, sendrName is not initialized....
senderName= oMessage.SenderName to be set in the for next loop

chspitAuthor Commented:
OK stefri thanks. Managed to figure it out.
Why dont you post your code?

chspitAuthor Commented:
Sure, why not. Here it comes...

Sub SaveAttachment()

    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)

If Inbox.Items.Count = 0 Then
    Exit Sub
End If
    For Each Item In Inbox.Items
        For Each Atmt In Item.Attachments
            If Right(Atmt.FileName, 3) = "doc" Then
                FileName = "C:\Documents and Settings\user\folder\" & _
                    Mid(Item.SenderName, 4, 8) & "_" & Atmt.FileName
                    Atmt.SaveAsFile FileName
            End If
        Next Atmt
    Next Item

    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
End Sub
I would not rely on the length of the attachment unless it is a robot which sends the attachment. I would use the real length of the filename
If you want to accelarate the process
check the unread items only (avoid Item which is referring an Outlook item)

for each itm in Inbox.Items

   if itm.unread = true and itm.attachments.count > 0 then
      itm.unread = false
      ' save the attachments
   end if

chspitAuthor Commented:
One more thing,
a message box appears to ask me if I want to allow the program to access the email addresses stored in the Outlook folder.How can I get rid of this message, programmatically?
You are hitting the security introduced by MS
Use redemption dll
Check Objects link, at the end of the page, an example shows how to use it for mail item

In short, you create an instance of a safeItem and copy the item from your items collection to the safeItem


I started this off and pointed you in the right direction but I get nothing for that?  That doesn't help my quest for the million point club very much.  Oh well, I thought setfri could handle it.  You can make it up to me by buying my book in June.
Stefri, your copy is on the way as soon as I get my pre-release copies.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.