2012-06-26 13 views
0

我想寫一個腳本,將附件保存到一個目錄。當我運行它時,出現「類型不匹配」錯誤消息,並且「Set olAtt = olMi.Attachments」行被突出顯示。有人可以建議嗎?設置outlook.attachment =郵件項目附件

非常感謝! abhisek

Sub SaveAttachments() 

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

Set olApp = New Outlook.Application 
Set olNs = olApp.GetNamespace("MAPI") 
Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 
Set MoveToFldr = Fldr.Folders("Survey") 
MyPath = "C:\Users\temp" 


For i = (Fldr.Items.Count - 150) To Fldr.Items.Count 

    Set olMi = Fldr.Items(i) 
    If InStr(1, olMi.Subject, "Survey") > 0 Then 
     j = olMi.Attachments.Count 
     Set olAtt = olMi.Attachments 
     filesavename = MyPath & olAtt.Filename 
     olAtt.SaveAsFile filesavename 

     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 

回答

1

olAtt被聲明爲Outlook.Attachment(單數)。

olMi.Attachments是附件的集合。