Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Rename an attachment.  (Read 2043 times)

0 Members and 1 Guest are viewing this topic.

sriramus

  • Guest
Rename an attachment.
« on: March 06, 2006, 07:14:32 AM »
Hi All,

I want to rename my email attachment received to attachment name same as the sender address.

In more detail:  If my email is received from the address say [email protected] and the attachment name is skills.doc i want to extract the attachment and rename it from skill.doc to [email protected]

Below is my code to save the attachment with the attached file name.  and i want this attachement to renamed to what ever the variable strReport contains each time the loop runs.

The below code that works fine:

Sub SendersInFolder()

  Dim ns As Outlook.Namespace
  Dim Inbox As Outlook.MAPIFolder
  Dim Item As Outlook.MailItem
  Dim strReport As String
  Dim Atmt As Attachment
  Dim FileName As String
  Dim i As Integer
 
  ' Get the current Folder
  Set ns = GetNamespace("MAPI")
  Set Inbox = ActiveExplorer.CurrentFolder
  ' Check for messages in Folder
  If Inbox.Items.Count = 0 Then
    strReport = "No Mail Items in current Folder"
  Else
    For Each Item In Inbox.Items
      ' Get the Sender's name and Email address
      strReport = strReport & Item.SenderName & vbCrLf
      MsgBox strReport

 
 i = 0
 
    For Each Atmt In Item.Attachments
       FileName = "D:\HR\Email Attachments\" & Atmt.FileName   '#1
       MsgBox FileName                                                            
       Atmt.SaveAsFile FileName                                                '#2
       i = i + 1
      
    Next Atmt
 Next Item
 End If
  
 If i > 0 Then
    MsgBox "I found " & i & " attached files." _
       & vbCrLf & "I have saved them into the D:\HR\Email Attachments folder." _
       & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
 Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
    "Finished!"
End If

'GetAttachments_err:
   'MsgBox "An unexpected error has occurred." _
      '& vbCrLf & "Please note and report the following information." _
      '& vbCrLf & "Macro Name: GetAttachments" _
      '& vbCrLf & "Error Number: " & Err.Number _
      '& vbCrLf & "Error Description: " & Err.Description _
      ', vbCritical, "Error!"
  ' Resume GetAttachments_exit


GetAttachments_exit:
   Set Atmt = Nothing
   Set Item = Nothing
   Set ns = Nothing
  
  ' Clean Up
  Set Inbox = Nothing
  Set ns = Nothing
  Set Item = Nothing
  MsgBox "done"
End Sub

I tried modifying the above code  in #1 as below line and tried
FileName = "D:\HR\Email Attachments\" & strReport

#2
Atmt.SaveAsFile strReport

I am getting error which says Automation error                                            

Can anyone help me with this please

Thanks in advance

soybean



    Genius
  • The first soybean ever to learn the computer.
  • Thanked: 469
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Rename an attachment.
« Reply #1 on: March 07, 2006, 08:37:51 AM »
Ever use newsgroups?  They're similar to forums, but they're organized around very specific subject areas.  So, they're a good place to get some responses to very technical questions like yours.  You can use Outlook Express as a newsgroup reader.  I'd suggest this newsgroup: microsoft.public.outlook.program_vba

sriramus

  • Guest
Re: Rename an attachment.
« Reply #2 on: March 07, 2006, 08:44:35 AM »
Hi soybean,

Thanks for your time taken to help me out and refering to another site.

Many Thanks again.