Friday, October 22, 2010

Import iPhone Notes to Evernote via Outlook

Here's a simple way to import your iPhone notes to Evernote. This solution requires Microsoft Outlook, so it will only work on the PC. Note this could just as easily be used for importing Outlook Notes into Evernote without an iPhone... just skip the first step! The main benefit of this method is that it will retain the creation date of all of your notes.
  1. Sync your iPhone notes with Outlook and make sure all your notes are in there.
  2. In Outlook, go to Tools > Macro > Visual Basic Editor (Alt+F11)
    You will see that Microsoft Visual Basic has opened.
  3. There should now be a blank document where you can type code. If you do not see this, expand the Project1 tree on the left (in the Project panel) until you get to ThisOutlookSession, and double click it.
  4. Paste the code at the bottom of this post after any existing code (you may or may not have code there), and save the document.
  5. Close Microsoft Visual Basic.
  6. Allow the macro to run by doing one of the following:
    • To temporarily allow all macros to run (easier), do the following:
      1. Go to Tools > Macro > Security...
      2. Change the Security Level to Medium or Low.
      3. Don't forget to change it back to High after you are done with this!
    • To create a certificate that will be used to sign your macro (safer), do the following:
      1. Start > All Programs > Microsoft Office > Microsoft Office Tools > Digital Certificate for VBA Projects
      2. Follow the steps to create a certificate, and name it whatever you want.
      3. Open Microsoft Visual Basic again (Alt+F11) in Outlook.
      4. Tools > Digital Signature > Choose...
      5. Choose the certificate you just created.
      6. Press OK and close Microsoft Visual Basic.
      7. When prompted in the next steps if you want to run this macro, remember to allow it and to choose to trust this publisher.
  7. If you changed any security settings, you should restart Outlook. When prompted if you want to save changes, choose yes.
  8. Run the macro by doing the following:
    1. Tools > Macro > Macros... (Alt+F8)
    2. Choose ThisOutlookSession.exportNotesToEvernote
    3. Press Run.
  9. You should now have a file in your My Documents folder called OutlookNotes.enex.
  10. Open up the Evernote desktop client, and do the following:
    1. Choose File > Import > Evernote Export Files...
    2. Choose the OutlookNotes.enex file located in My Documents.
  11. You should now have a new notebook with all of your notes.
  12. Tip: If you want to tag all of these new notes with the same tag, you can select this notebook, then select all the notes in it, and then drag them onto a tag.
Here's the code for Microsoft Visual Basic:
Function pad(ByVal n, ByVal l) As String
  pad = Right("000000000" & n, l)
End Function

Function enDateTime(ByVal d) As String
  ' Sample date: 20101021T205209Z
  enDateTime = Year(d) & pad(Month(d), 2) & pad(Day(d), 2) & "T" & pad(Hour(d), 2) & pad(Minute(d), 2) & pad(Second(d), 2) & "Z"
End Function

Function HTMLEncode(ByVal Text As String) As String
  ' Slightly modified version of
    Dim i As Integer
    Dim acode As Integer
    Dim repl As String

    HTMLEncode = Text

    For i = Len(HTMLEncode) To 1 Step -1
        acode = AscW(Mid$(HTMLEncode, i, 1))
        Select Case acode
            Case 32
                ' repl = " "
            Case 34
                repl = """
            Case 38
                repl = "&"
            Case 60
                repl = "<"
            Case 62
                repl = ">"
            Case 32 To 127
                ' don't touch alphanumeric chars
            Case 13
              ' don't touch new lines
            Case 10
              ' don't touch new lines
            Case Else
                repl = "&#" & CStr(acode) & ";"
        End Select
        If Len(repl) Then
            HTMLEncode = Left$(HTMLEncode, i - 1) & repl & Mid$(HTMLEncode, _
                i + 1)
            repl = ""
        End If
End Function

Sub exportNotesToEvernote()
  FileName = Environ$("USERPROFILE") & "\My Documents\OutlookNotes.enex"
  File = FreeFile()
  Open FileName For Output As File
  Print #File, "<?xml version=""1.0"" encoding=""UTF-8""?>" _
   & "<!DOCTYPE en-export SYSTEM """">" _
   & "<en-export export-date=""" & enDateTime(Now) & """ application=""Evernote/Windows"" version=""3.5"">"
  Tag = ""
  ' Uncomment this line to set a tag for all notes and (replace the My Tag with the name of the tag you want)
  ' Tag = "<tag>" & xmlEscape("iPhone Notes") & "</tag>"
  Set Notes = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderNotes)
  For i = 1 To Notes.Items.Count
    Set note = Notes.Items(i)
    Subject = note.Subject
    Subject = Left(Subject, 64)
    Subject = HTMLEncode(Subject)
    Body = note.Body
    Body = HTMLEncode(Body)
    Body = Replace(Body, vbNewLine, "<" & "br/>" & vbLf)
    Print #File, "<note><title>" & Subject & "</title><content>" _
      & "<![CDATA[<?xml version=""1.0"" encoding=""UTF-8""?>" _
      & "<!DOCTYPE en-note SYSTEM """">" _
      & "<en-note>" & Body _
      & "</en-note>]]></content><created>" & enDateTime(note.CreationTime) & "</created><updated>" & enDateTime(note.CreationTime) & "</updated>" & Tag & "</note>"
    ' uses the note's creation time for both the modification and creation time in Evernote
  Print #File, "</en-export>"
  Close #File
End Sub
Understanding how the dates work:

When you look at notes on the iPhone, you only see their modification date. Behind the scenes, Apple is also storing their creation date, though. When you import the notes to Outlook, it will set the creation date to the time that Apple recorded as the note being created, and it will set the modification date to the current time. Thus, the modification time in Outlook is useless, and there is no way (using Outlook) to get the same exact dates you see on the iPhone. For this reason, I chose to completely ignore Outlook's modification date and I use the creation date for both the created and modification dates in Evernote.


  1. I work fine for me.
    But i want to import not only the default folder note but also the other folder note.

    "Set Notes = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderNotes)"

    What can i modify for import other folder then de default ?
    Excuse my bad english i am a French.


  2. That you for posting this. It saved me a lot of time last night, and it worked. If I knew more VB I would propose taking the outlook category and making it the tag. That seems like a logical improvement.

  3. Excellent. It didn't work for me at first, Evernote reporting an error on a note title, so doing a search for <title></title> and adding Unnamed Note inbetween the opening and closing tags fixed it and it imported fine.

  4. Didn't work for me. I receive a message "can't find the way" or something like this when trying to run ThisOutlookSession.exportNotesToEvernote

  5. It WORKED!! :-) Thanks a lot!! 5 star feed back ;-)