【问题标题】:Run-time Error 424 Object Required运行时错误 424 需要对象
【发布时间】:2014-09-03 15:47:48
【问题描述】:

我正在为 Outlook 2010 编写一个宏。即使是第一次,我也会在 for each 循环中遇到错误,并且 oFolder 确实包含一个文件夹。 顺便说一句,SaveAttachments 第一次运行正确,只是第二次轰炸。

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

Sub SaveAttachments(ByVal oParent As 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

结束子

【问题讨论】:

  • 您的哪一行代码引发了该错误?什么是 SaveAttachments 子?
  • 对于 objInbox.Folders 中的每个 oFolder SaveAttachments (oFolder) 下一步
  • 单步执行代码时,哪个对象为空?
  • 这就是我不明白的。 o调用 saveAttachments 时,Folder 不为空。
  • 那么你可以单步执行代码吗?

标签: vba outlook


【解决方案1】:

你混配了调用SaveAttachments的方法

二选一

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

SaveAttachments objInbox        ' <--- No brackets

SaveAttachments oFolder         ' <--- No brackets

【讨论】:

    猜你喜欢
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多