First trial to create note from Outlook

If the images are in a <img> tag they are already visible in Joplin, for example company logos, pictures in the mails of newspapers.

"To choose notebook, you can use InputBox function 1.

To clip another email fields, you need to choose mail properties and add them to the send method.

I’ll try to code both later on, and post the code here."

Any news regarding this?

You could try something like this:

 sEscapedBody = EscapeBody( _
          "Date: " & objItem.ReceivedTime & "<br>" _
          & "To: " & objItem.To & "<br>" _
          & objItem.HTMLBody)

Here is the documentation for the properties of the MailItem object.

1 Like

Here is the complete code that:

  1. Prompt you to choose Joplin folder,
  2. Add ReceivedTime and To as per @Tekki comment above,
  3. Send the email information to Joplin.
Public Sub SendToJoplin()
    Dim sToken As String, sURL As String
    Dim sURLNotes, sURLResources, sEscapedBody, sJSONString, sFolderID As String
    Dim objItem As Outlook.MailItem
    sURL = ""
    sURLNotes = sURL & "/notes?token=" & sToken
    sURLResources = sURL & "/resources?token=" & sToken

    For Each objItem In ActiveExplorer.Selection
        sEscapedBody = EscapeBody( _
                "Date: " & objItem.ReceivedTime & "<br>" _
                & "To: " & objItem.To & "<br>" _
                & objItem.HTMLBody)
        sFolderID = GetFoldersFromJoplin(sToken, sURL)
        'If objItem.Attachments.Count > 0 Then
        'For Each Attachment In objItem.Attachments
            'With CreateObject("MSXML2.XMLHTTP")
                '.Open "POST", sURLResources, False
                '.Send "{ ""title"": """ & objItem.ConversationTopic & """" _
                '& ", ""parent_id"": ""972fbd8370444478b8af890300b26968""" _
                '& ", ""body_html"": """ & sEscapedBody & """" _
                '& " }"
                'Do Until .ReadyState = 4: DoEvents: Loop
                    'sJSONString = .ResponseText
            'End With
        With CreateObject("MSXML2.XMLHTTP")
            .Open "POST", sURLNotes, False
            .Send "{ ""title"": """ & objItem.ConversationTopic & """" _
            & ", ""parent_id"": """ & sFolderID & """" _
            & ", ""body_html"": """ & sEscapedBody & """" _
            & " }"
            Do Until .ReadyState = 4: DoEvents: Loop
                sJSONString = .ResponseText
        End With
    'Debug.Print sJSONString 'Uncomment to see joplin response
End Sub

Private Function EscapeBody(sText As String)
    EscapeBody = sText
    EscapeBody = Replace(EscapeBody, "\", "\\")                 'Backslash is replaced with \\
    EscapeBody = Replace(EscapeBody, Chr(34), "\" & Chr(34))    'Double quote is replaced with \"
    EscapeBody = Replace(EscapeBody, vbCr, "\r")                'Carriage return is replaced with \r
    EscapeBody = Replace(EscapeBody, vbLf, "\n")                'Newline is replaced with \n
    EscapeBody = Replace(EscapeBody, Chr(8), "\b")              'Backspace is replaced with \b
    EscapeBody = Replace(EscapeBody, Chr(12), "\f")             'Form feed is replaced with \f
    EscapeBody = Replace(EscapeBody, vbTab, "\t")               'Tab is replaced with \t
End Function

Private Function GetFoldersFromJoplin(sToken As String, sURL As String)
    'Input token, url
    'Output folder id
    Dim sJSONString, sMessage, sTitle, sDefault, sMyChoice As String
    Dim vJSON As Variant
    Dim sState As String
    Dim aData(), aHeader()
    Dim i As Integer

    sURL = sURL & "/folders?token=" & sToken
    'Get folders list
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", sURL, False
        Do Until .ReadyState = 4: DoEvents: Loop
            sJSONString = .ResponseText
    End With
    'Parse JSON response
    JSON.Parse sJSONString, vJSON, sState
    JSON.ToArray vJSON, aData(), aHeader()
    'Dsiplay a choices of folders
    'Set prompt
    sMessage = "Enter a value between " & LBound(aData) & " To " & UBound(aData)
    For i = LBound(aData) To UBound(aData)
        sMessage = sMessage & Chr(10) & i & " " & aData(i, 2)
    Next i
    sTitle = "Choose Joplin folder..."    'Set title
    sDefault = "1"    'Set default
    sMyChoice = InputBox(sMessage, sTitle, sDefault)
    GetFoldersFromJoplin = aData(sMyChoice, 0)
End Function


  1. For this code to work, you need to download and import this JSON.bas to your Outlook.
  2. Make sure below References are enabled (Tools > References):
1 Like

Many thanks!
Looks great, but unfortunately I got the error:

Microsoft Visual Basic for Applications

Laufzeitfehler ‘424’:

Objekt erforderlich

OK Hilfe

@iknow79 Sorry, I re-edited the post and pasted the code again. Thanks to copy it into your outlook and try again.

Thanks, it works now with the first code, I did not know that I have to enable the references… I can not paste the second one, because the layout is “destroyed”. I´m not a programmer, getting this stuf to run is just a hobby, so it will be hard for me to reeit the 2nd code you posted.

You could also try to convert the images to dataUrl format, and then you can embed them in the note directly.

I`m not talking of images, but the last “destoryed code” above

@iknow79 What do you mean by layout destroyed and the 2nd code?

@rami.sedhom Maybe you could create a Gist with the final code and link to it from the first message in this thread?

This one...

@iknow79 Can you please put code in a proper code block? Don’t you see how messy your posts look like?

My post??? I posted it in the same way rami posted it above. That`s why I said it is “destroyed”. Take a look above. Please have a look first before creating comments like this…

I did, maybe you are blind, but your posts are the only ones that look like that.

Here is the gist:

@Tekki How to link it from the first post? It seems I can’t edit old posts!

Don’t know, I thought it is possible. When I go to my older posts I have an edit this post button.
Thanks for all your work! This was my most missed feature from OneNote.

Great work, thanks a lot. But running the code I only get 5 notebooks as a clipping destination. But I have created a lot more:)
Where can this be changed?

May be because you are an old member with lot of badges :smiley:

This is true, I create 6 notebooks and api GET folders returned only 5!
There is noting in the code limiting the number of folders, and there is nothing in the api documentation about limitation in response. So I'm not sure what's the solution.

@laurent, @Tekki, @tessus any advice about this limitation?

Another question:
Right now it seems, that only “to” , the date and the mail text are sent to Outlook.
Would it be possible to sent the whole header, like this:

From: xy xy
Sent: Samstag, 9. November 2019 22:29
Subject: bla bla

This is the mail text