Click here to Skip to main content
13,087,941 members (65,957 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.
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.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 26-Mar-13 6:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try: Dim olMi As Variant
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100