vbafile-iooutlookstring-concatenationoutlook-2003

Creating a single file from multiple notes in Outlook VBA


Currently, my VBA code creates a single file for each note. Here's some simplified sample code:

Sub saveNotes()
   Set myNote = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderNotes)
   For ibi = 1 To myNote.Items.Count
     fname = myNote.Items(ibi).Subject
     myNote.Items(ibi).SaveAs "C:\Temp\" & fname & ".txt", 0
   Next
End Sub

Instead of creating a single file for each note, I want to create a single file for all the notes. I'm looking for the most efficient method to concatenate these note's content and write them to a single file.

To give you an idea of the amount of data and how efficient it should be, there are hundreds of notes with an average size of 1024 chars.


Solution

  • This code will open a text file for output and write the modified time and body of each note into the text file. The file will be in your My Documents folder and will be named AllNotesYYYYMMDD.txt. You might change that path based on your operating system and where you actually want the file stored.

    Sub SaveNotes()
    
        Dim fNotes As MAPIFolder
        Dim ni As NoteItem
        Dim sFile As String
        Dim lFile As Long
    
        Set fNotes = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderNotes)
        lFile = FreeFile
        sFile = Environ$("USERPROFILE") & "\My Documents\AllNotes" & Format(Date, "yyyymmdd") & ".txt"
    
        Open sFile For Output As lFile
    
        For Each ni In fNotes.Items
            Print #lFile, "Modified:" & vbTab & ni.LastModificationTime & vbNewLine & ni.Body
            Print #lFile, "-------------------------------"
        Next ni
    
        Close lFile
    
    End Sub