2014-09-03 118 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 
相关问题