Click here to Skip to main content
11,921,138 members (61,220 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VBScript
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 7: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
0 OriginalGriff 583
1 Dave Kreskowiak 245
2 phil.o 160
3 KrunalRohit 134
4 F-ES Sitecore 130
0 OriginalGriff 6,951
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,171
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2015
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