Outlook VBA中的错误文件名错误

时间:2013-09-11 03:33:02

标签: outlook outlook-vba

我在Microsoft Outlook VBA中创建了一个子例程来创建一个csv文件,该文件列出了我的收件箱及其子文件夹中的所有邮件。我只是在查看我的收件箱时让宏工作,但是当我尝试循环浏览子文件夹时,它会在我正在尝试写入的文件中返回“错误的文件名错误”。因此,processFolder函数中的“Write #ff”行基本上会导致错误。关于文件名#ff的范围,我有什么遗漏吗?谢谢

Sub Response_Log()

Dim myItem As Object
Dim msg As MailItem
Dim myFolder As Folder
Dim mysubfolder As Outlook.Folder
Dim myNamespace As NameSpace
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.Folders("My mailbox").Folders("Inbox")

Dim resFile As String
    resFile = "myfilepath\Response_Log.csv"
Dim ff As Byte
    ff = FreeFile()
Open resFile For Append As #ff
Write #ff, "Sender Name"; "Sender Email Address"; "Received Time"; "Subject"; "Response Status and Response Time"
processFolder myFolder

Close #ff

End Sub

Function processFolder(oParent As Outlook.MAPIFolder)

    Dim oFolder As Outlook.MAPIFolder
    Dim oMail As Object
    Dim Respond As String
    Dim NoRespond As String
        NoRespond = "Not Yet Replied"


    For Each oMail In oParent.Items
        If TypeOf oMail Is MailItem Then
           If Mid(oMail.SenderEmailAddress, 1, 3) <> "/O=" Then
                If oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 102 Or oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 103 Then
                    Respond = "Replied on " & oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10820040")
                    Write #ff, oMail.SenderName; oMail.SenderEmailAddress; oMail.ReceivedTime; oMail.Subject; Respond
                Else                        
                    Write #ff, oMail.SenderName; oMail.SenderEmailAddress; oMail.ReceivedTime; oMail.Subject; NoRespond
                End If
            End If
        End If
    Next

    If (oParent.Folders.Count > 0) Then
        For Each oFolder In oParent.Folders
            processFolder oFolder
        Next
    End If

结束功能

1 个答案:

答案 0 :(得分:0)

ff中的文件编号Response_Log似乎已定义,您正试图在processFolder使用。这不会很好地结束: - )

你可能最好更改processFolder,以便它接受一个额外的参数,即要写入的文件号。

然后确保从Response_LogprocessFolder本身的递归调用中传递它。类似的东西:

Sub Response_Log()
    :
    processFolder myFolder,ff
:
Function processFolder(oParent As Outlook.MAPIFolder,ff as Byte)
    :
    processFolder oFolder,ff

您可能还想检查这些调用的语法。我做VBA已经有一段时间了,但是使用可选的call语句和括号可能会出现一些问题。它可能需要更像是:

call processFolder (oFolder,ff)