Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I found the following script but i get a runtime error 13 type mismatch on the line that says "Set olMi = Fldr.Items(i)" Thanks for your help.
VB
Sub SaveAttachments()

    Dim olApp As Outlook.Application
    Dim olNs As NameSpace
    Dim Fldr As MAPIFolder
    Dim MoveToFldr As MAPIFolder
    Dim olMi As MailItem
    Dim olAtt As Attachment
    Dim MyPath As String
    Dim i As Long

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    Set MoveToFldr = Fldr.Folders("CompSurv")
    MyPath = "C:\My Documents\Completed Survey\"

    For i = Fldr.Items.Count To 1 Step -1
        Set olMi = Fldr.Items(i)
        If InStr(1, olMi.Subject, "Completed Survey") > 0 Then
            For Each olAtt In olMi.Attachments
                If olAtt.FileName = "Test.xls" Then
                    olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls"
                End If
            Next olAtt
            olMi.Save
            olMi.Move MoveToFldr
        End If
    Next i

    Set olAtt = Nothing
    Set olMi = Nothing
    Set Fldr = Nothing
    Set MoveToFldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing

End Sub
Posted

Try: Dim olMi As Variant
 
Share this answer
 
Comments
namerg 26-Mar-13 13:58pm    
@Zoltán: PERFECT. But when saving the attachment, it saves it as my profile name, not the attachment name, i did send the attachment as Test.xlsx
Zoltán Zörgő 26-Mar-13 14:02pm    
You wrote this: olAtt.SaveAsFile MyPath & olMi.SenderName & ".xls", so if you sent this mail to yourself, the file name will be yours. Just a note: I suggest, you normalize the SenderName before using it in a file name.
namerg 26-Mar-13 14:21pm    
Got it. I did replace that line with olAtt.SaveAsFile MyPath & olAtt.FileName
Try:
VB
For i = (Fldr.Items.Count-1) To 0 Step -1
       Set olMi = Fldr.Items(i)

Type mismatch means that the object returned does not match what you are setting to.

If above does not resolve, do JavaScript debugging and see for what value and when is the error happening. Try to look around that and troubleshoot.
 
Share this answer
 
Comments
namerg 26-Mar-13 14:36pm    
Thanks Sandeep

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900