Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Code that saves attachments when arrived. in 2 Folders. need and addition.

hi,

Code that saves attachments when arrived. in 2 Folders. need and addition.
at present the 2nd location files re saved only if the folder exists. if folder not available it does not save them.

Regards
sharath
Sub SaveAttachment_NoStrip(ByRef mai As Outlook.mailitem) 
Dim objItem As Object 
Dim mailAtt As Attachment 
Dim intAtt As Integer 
Dim saver1 As String 
Dim constsaver2 As String 
Dim saver2 As String 
Dim constsaver1 As String 
Dim fn As String 
Dim ft As String 
Dim Subject As String 
Dim del As Variant 
Const sendereMailTrigger As String = "India.Alert@plc.com" 
Const saveFolder1 As String = "D:\SophosMailAttachments\" 
Const saveFolder2 As String = "\\servername\foldername\foldername\" 
       
     On Error GoTo exitsub 
     If LCase(mai.sendereMailAddress) <> LCase(sendereMailTrigger) Then Exit Sub 
    If mai.Attachments.count > 0 Then 
        If Right(saveFolder1, 1) = "\" Then 
            constsaver1 = saveFolder1 
            constsaver2 = saveFolder2 
        Else 
            constsaver1 = saveFolder1 & "\" 
            constsaver2 = saveFolder2 & "\" 
        End If 
        constsaver1 = constsaver1 & Format(Date, "yyyy-mm-dd") & "\" 
        constsaver2 = constsaver2 & Format(Date, "yyyy-mm-dd") & "\" 
        md constsaver1, True 
        md constsaver2, True 
        For intAtt = 1 To mai.Attachments.count 
            Subject = Replace(mai.ConversationTopic, """", " ") 
            For Each del In Array("/", ":", "*", "?", "<", ">", "|") 
                Subject = Replace(Subject, del, " ") 
            Next 
            Subject = Left(Subject, 250 - 16 - Len(constsaver1)) 
            saver1 = constsaver1 & Subject & "_" & Left(mai.Attachments.Item(intAtt).FileName, InStr(mai.Attachments.Item(intAtt).FileName, ".") - 1) & "_" & Format(Date, "yyyy-mm-dd") & "." & Right(mai.Attachments.Item(intAtt).FileName, Len(mai.Attachments.Item(intAtt).FileName) - InStr(mai.Attachments.Item(intAtt).FileName, ".")) 
            mai.Attachments.Item(intAtt).SaveAsFile saver1 
            Subject = Left(Subject, 250 - 16 - Len(constsaver2)) 
            saver2 = constsaver2 & Subject & "_" & Left(mai.Attachments.Item(intAtt).FileName, InStr(mai.Attachments.Item(intAtt).FileName, ".") - 1) & "_" & Format(Date, "yyyy-mm-dd") & "." & Right(mai.Attachments.Item(intAtt).FileName, Len(mai.Attachments.Item(intAtt).FileName) - InStr(mai.Attachments.Item(intAtt).FileName, ".")) 
            mai.Attachments.Item(intAtt).SaveAsFile saver2 
        Next 
    End If 
exitsub: 
End Sub

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Hi, Sharath.

It's to be expected that a macro can't save something to a folder that doesn't exist.  Does that mean that you want the macro to create the folder if it doesn't exist?
Avatar of bsharath

ASKER

Yes For the first location it does create a folder just the 2nd location it does not create
"For the first location it does create a folder"
I don't see how.  There are no commands in the script for testing to see if the folder exists or for creating it if it doesn't.  Are you sure this is the correct script and/or the complete script?
There's a call to a subroutine called "md" that might be making the folders.  That code isn't included in what you posted.
David do you ,ean there is some code in other modules.
For now the first location folder is created and attachments are saved into them.
"do you ,ean there is some code in other modules."
There must be.  There is NO CODE in what you posted that will test to see if a folder exists or create one.  Lines 29 and 30 make reference to a subroutine called "md" that is not present in what you posted.  MD is the name of a DOS command called "Make Directory".  VBScript cannot run a DOS command directly and MD is not a command in VB.  Therefore there must be a subroutine that's missing from what you posted.  I can't do anything to fix the problem without being able to see ALL the code that's involved.

"For now the first location folder is created and attachments are saved into them."
I don't doubt that.  There are three possibilities.  

1.  The second path is invalid.  Try changing the path and see what happens.
2.  The code in the missing MD subroutine can't handle a UNC style path.  I can't tell if that's the case without seeing the code that's in MD.
3.  You don't have sufficient permissions to create a folder in the location specified by the second path.
David
Could not find the remaining but will still try again
First the folder is created and files saved within it in D Drive
In the UNC when i create the same folder named in the path and wait for the mail to arrive the files are saved.
can you change the code to check if folder exists else create one
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America 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
Thanks a lot David works perfect
You're welcome.