2014-09-03 156 views
1

即時通訊編寫一個宏爲Outlook 2010.即使第一次和oFolder確實包含文件夾,我得到每個循環的錯誤。 012W順便說一下,SaveAttachments在第一次爆炸時第一次正確運行。運行時錯誤424對象要求

Public Sub processFolder() 

    Set objNS = Application.GetNamespace("MAPI") 
    Dim oParent As Outlook.MAPIFolder 
    Dim oFolder As Outlook.MAPIFolder 
    Dim FolderName As String 

    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)   

    SaveAttachments (objInbox) 


    If (objInbox.Folders.Count > 0) Then 
     For Each oFolder In objInbox.Folders 
      SaveAttachments (oFolder) 
     Next 
    End If 
End Sub 

子SaveAttachments(BYVAL oParent作爲Outlook.MAPIFolder)

'Declaration 
Dim myItems, myItem, myAttachments, myAttachment As Object 
Dim myOrt As String 
Dim myOlApp As New Outlook.Application 

myOrt = "C:\attachments" 

On Error Resume Next 

'for all items do... 
For Each myItem In oParent.Items 

    'point on attachments 
    Set myAttachments = myItem.Attachments 

    'if there are some... 
    If myAttachments.Count > 0 Then 

     'add remark to message text 
     myItem.Body = myItem.Body & vbCrLf & _ 
      "Removed Attachments:" & vbCrLf 

     'for all attachments do... 
     For i = 1 To myAttachments.Count 

      'save them to destination 
      myAttachments(i).SaveAsFile myOrt & _ 
       myAttachments(i).DisplayName 

      'add name and destination to message text 
      myItem.Body = myItem.Body & _ 
       "File: " & myOrt & _ 
       myAttachments(i).DisplayName & vbCrLf 

     Next i 

     'for all attachments do... 
     While myAttachments.Count > 0 

      'remove it (use this method in Outlook XP) 
      'myAttachments.Remove 1 

      'remove it (use this method in Outlook 2000) 
      myAttachments(1).Delete 

     Wend 

     'save item without attachments 
     myItem.Save 
    End If 

Next 

'free variables 
Set myItems = Nothing 
Set myItem = Nothing 
Set myAttachments = Nothing 
Set myAttachment = Nothing 
Set myOlApp = Nothing 
Set myOlExp = Nothing 
Set myOlSel = Nothing 

結束子

+0

你哪行代碼引發了這個錯誤? SaveAttachments子是什麼? – 2014-09-03 15:50:56

+0

對於每個oFolder在objInbox.Folders ** SaveAttachments(oFolder)** Next – DES 2014-09-03 16:04:18

+0

當您通過代碼時,哪個對象爲null? – 2014-09-03 17:19:13

回答

1

您已混合和匹配主叫SaveAttachments的方法

選擇一個或另一個

Call SaveAttachments(objInbox) ' <--- Call requires brackets 

SaveAttachments objInbox  ' <--- No brackets 

SaveAttachments oFolder   ' <--- No brackets 
相關問題