Link to home
Start Free TrialLog in
Avatar of chspit
chspit

asked on

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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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.
Avatar of chspit
chspit

ASKER

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?
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"
                    .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.
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
Avatar of chspit

ASKER

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

Can you please guide me to some documentation. Thanks
ASKER CERTIFIED SOLUTION
Avatar of stefri
stefri
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Slink9,
In your example, sendrName is not initialized....
senderName= oMessage.SenderName to be set in the for next loop

stefri
Avatar of chspit

ASKER

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

Stefri
Avatar of chspit

ASKER

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
      itm.save
   end if

Stefri
Avatar of chspit

ASKER

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
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

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.