save attachment

Hi,

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
chspitAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
stefriConnect With a Mentor Commented:
Oops...
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

Stefri

0
 
sirbountyCommented:
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:

http://www.slipstick.com/_fdse/search.pl
http://www.outlookcode.com/d/vbscript.htm
http://www.outlookcode.com/d/code/copyatts.htm

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.
0
 
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?
0
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.

 
sirbountyCommented:
I don't believe so...another expert may come along and shed some insight - but Outlook VBA is, imho, very cumbersome compared to VBA...
0
 
slink9Commented:
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"
                    .Delete
            End If
        End With
    Next
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.
0
 
stefriCommented:
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)

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

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

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

Stefri
0
 
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
0
 
stefriCommented:
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
      itm.save
   end if

Stefri
0
 
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?
0
 
stefriCommented:
You are hitting the security introduced by MS
Use redemption dll
http://www.dimastr.com/redemption/download.htm
http://www.dimastr.com/redemption/home.htm
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

Stefri

0
 
slink9Commented:
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.
0
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.