0
votes

I have made a subroutine in Microsoft Outlook VBA to create a csv file which lists all the messages in my inbox and its subfolders. I got the macro to work when just looking at my inbox, but when I tried looping through the subfolders, its returning a "bad file name error" in reference to the file I'm trying to write to. So basically the "Write #ff" lines in the processFolder function are causing an error. Is there something I'm missing about the scope of the file name #ff? Thanks

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

End Function

1

1 Answers

0
votes

The file number ff appears to be defined in Response_Log and you're trying to use it in processFolder. That's not going to end well :-)

You're probably best off changing processFolder so that it accepts an extra parameter, the file number to write to.

Then make sure you pass it, both from Response_Log and in the recursive call from processFolder itself. Something like:

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

You may also want to check the syntax of those calls as well. It's been a while since I did VBA but there are some issues that may arise with the use of the optional call statement and parentheses. It may need to be something more like:

call processFolder (oFolder,ff)