esdee303 Posted December 21, 2010 Share Posted December 21, 2010 Hello,i'm trying to save my attachments from outlook to a local folder. This is no problem, but what I also want to do is name the attachment according to a value in the body of the eMail. Somwhere in the mail is a word (referentie '), from there on I need to count 18 characters and use that as the filename.This is what i got so far, saving is ok, when i use this code, the filename stays the sameSub SaveAllAttachments(objitem As MailItem) Dim objAttachments As Outlook.Attachments Dim strName, strLocation As String Dim dblCount, dblLoop As Double strLocation = "C:\test\" locatieBeginMrn = InStr(mailBody, "referentie '") mrn = Mid(locatieBeginMrn, 1, [20]) On Error GoTo ExitSub If objitem.Class = olMail Then Set objAttachments = objitem.Attachments dblCount = objAttachments.Count If dblCount <= 0 Then GoTo 100 End If For dblLoop = 1 To dblCount strName = objAttachments.Item(dblLoop).FileName strName = strLocation & mrn objAttachments.Item(dblLoop).SaveAsFile strName Next dblLoop objitem.Delete End If100ExitSub: Set objAttachments = Nothing Set objOutlook = NothingEnd SubDoes anyone know how to rename the attachment according to value in the body ?ThxSteven Link to comment Share on other sites More sharing options...
esdee303 Posted January 10, 2011 Author Share Posted January 10, 2011 The Solution is belowexplained: i search for a string "entie '"The I count 7 positions, and from ther i take the next 18 charactersSub SaveAllAttachmentsPLDA(objitem As MailItem) Dim objAttachments As Outlook.Attachments Dim strName, strLocation, mrn As String Dim dblCount, dblLoop As Double strLocation = "C:\Test\" locatieBeginMrn = InStr(objitem.Body, "entie '") mrn = Mid(objitem.Body, locatieBeginMrn + 7, 18) On Error GoTo ExitSub If objitem.Class = olMail Then Set objAttachments = objitem.Attachments dblCount = objAttachments.Count If dblCount <= 0 Then GoTo 100 End If For dblLoop = 1 To dblCount strName = strLocation & "release_" & mrn & ".pdf" objAttachments.Item(dblLoop).SaveAsFile strName Next dblLoop objitem.Save End If100ExitSub: Set objAttachments = Nothing Set objOutlook = NothingEnd Sub Hello,i'm trying to save my attachments from outlook to a local folder. This is no problem, but what I also want to do is name the attachment according to a value in the body of the eMail. Somwhere in the mail is a word (referentie '), from there on I need to count 18 characters and use that as the filename.This is what i got so far, saving is ok, when i use this code, the filename stays the sameSub SaveAllAttachments(objitem As MailItem) Dim objAttachments As Outlook.Attachments Dim strName, strLocation As String Dim dblCount, dblLoop As Double strLocation = "C:\test\" locatieBeginMrn = InStr(mailBody, "referentie '") mrn = Mid(locatieBeginMrn, 1, [20]) On Error GoTo ExitSub If objitem.Class = olMail Then Set objAttachments = objitem.Attachments dblCount = objAttachments.Count If dblCount <= 0 Then GoTo 100 End If For dblLoop = 1 To dblCount strName = objAttachments.Item(dblLoop).FileName strName = strLocation & mrn objAttachments.Item(dblLoop).SaveAsFile strName Next dblLoop objitem.Delete End If100ExitSub: Set objAttachments = Nothing Set objOutlook = NothingEnd SubDoes anyone know how to rename the attachment according to value in the body ?ThxSteven Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.