Solved

save attachment

Posted on 2004-04-03
15
908 Views
Last Modified: 2010-04-08
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
0
Comment
Question by:chspit
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
 

Author Comment

by:chspit
Comment Utility
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
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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
 
LVL 13

Expert Comment

by:stefri
Comment Utility
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
 

Author Comment

by:chspit
Comment Utility
What is >>>Mid(oMessage.Sendername , 4, Len(oMessage.Sendername ) - 3)

Can you please guide me to some documentation. Thanks
0
 
LVL 13

Accepted Solution

by:
stefri earned 250 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 13

Expert Comment

by:stefri
Comment Utility
Slink9,
In your example, sendrName is not initialized....
senderName= oMessage.SenderName to be set in the for next loop

stefri
0
 

Author Comment

by:chspit
Comment Utility
OK stefri thanks. Managed to figure it out.
0
 
LVL 13

Expert Comment

by:stefri
Comment Utility
Why dont you post your code?

Stefri
0
 

Author Comment

by:chspit
Comment Utility
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
 
LVL 13

Expert Comment

by:stefri
Comment Utility
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
 

Author Comment

by:chspit
Comment Utility
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
 
LVL 13

Expert Comment

by:stefri
Comment Utility
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
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Outlook Free & Paid Tools
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now