Jump to content

Automated Note Creation from Outlook/Windows via Powershell


Recommended Posts

I like to  use Evernote for making notes during meetings and calls, and have a particular note format that I like.  I wanted a way to automate the creation of new notes from Outlook that included inserting a title and date/time stamp, but also including the formatting of a note template.  I don't know if this is crazy or not, as one could argue you could just go into Evernote and create a new note and make a few changes.  Being a Office VBA person I tried this with an Outlook VBA macro but could not get that to work using the native Evernote Windows SDK.  I find it unbelieveable that there were no real working examples of VBA code creating notes...sorry for digressing. :(

After doing research I came across a post about using using the Evernote "enscript.exe" program to import notes that would preserve the formatting and tagging info that was stored with the exported note.  To use this effectively I had to come up with a way to customize the file that I was importing with a specific title and date/time stamp.  Having some Powershell script experience, I figured out a fairly straightforward way to do this that has possibilities for much more customization.

The solution was a short Outlook VBA macro that calls a Powershell script - both components that are available with every install of MS Office/Windows.  I've documented this as best I can - both in the macro and script.  Sorry if this is too much detail.

This assumes some Evernote, VBA and Powershell knowledge - use this script at your own risk.  I'm not including instructions how to program in VBA and Powershell, so Google this if you need help.

To utilize this, you should do the following - if you want to use it as is then just install the macros and Powershell script and run as is. If you want to customize, then do the following:

  •  In Evernote, create a note that contains the tagging and proper formatting you want with bullets, lines, etc.  When you define this keep in mind where you want to customize each newly created note.  In my case I wanted a custom title and a subheading that was in bold font followed by a list of indented bullets.  Yours can be whatever you want. Save this note and then export it to a .enex file.  Open this .enex file with a text editor and do the following:    
  • In order to get a correct created and updated dates on the imported note, you must delete the xml fields that contain the "created" and "updated" field info.  This link contains the instructions on how to do this.  
  • Change the text in the "title" field to a unique string - mine was "title_template", see the text below - so that in the Powershell script later we can do a find/replace.
  • Change the text "subject_template" in the "en-note" section to a unique string  - mine was "subject_template", see the text below - so that in the Powershell script later we can do a find/replace.
  • Save and close the .enex xml file.  Here's the resulting .enex xml file - 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20171025T221620Z" application="Evernote/Windows" version="6.x">
   <note>
      <title>title_template</title>
      <content><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">

<en-note><div><hr/><div><span style="font-family: &apos;Lucida Sans Unicode&apos;; font-weight: bold;">subject_template</span></div><div><ul><li><br/></li><li><br/></li></ul></div></div><div><hr/></div><div><br/></div></en-note>]]></content>
      <tag>!import</tag>
      <note-attributes>
         <author>YOUR NAME</author>
         <source>desktop.win</source>
         <source-application>evernote.win32</source-application>
      </note-attributes>
   </note>
</en-export>
  •  Here's what the raw template looks like if imported into Evernote - you can see the words "title_template" in the title bar, and "subject_template" in the subheading under the horizontal line.

Note template after import into Evernote - notice the formatting and tagging is preserved                

  •  In Outlook, paste the "create_evernote_note" macro code shown below into a module in your Outlook VBA project.  You must customize the variable CPWSScript to include the location where you are storing your Powershell script (mine is in a Dropbox folder).  You should create a button on your menu bar to make it easy to execute - see this for how to do that.
Public Sub create_evernote_note()

'________________________________________________________________________________________________________________
'   using a evernote .enex template, create a note in the instor notebook
'   Do most of the heavy lifting by calling Powershell script that imports
'   a note, adds custom data to it, and stores it in the desired notebook.
'________________________________________________________________________________________________________________
	
'________________________________________________________________________________________________________________
'   Declare variable with name and location of the powershell script to run.
'   Change this location as needed to where your script is located.
'________________________________________________________________________________________________________________
	Dim strMyTitle       As String
	Const cPWSScript     As String = "C:\Users\USER\dropbox\powershell_scripts\create_evernote_note.ps1"
'________________________________________________________________________________________________________________
'   Prompt user for the note title
'________________________________________________________________________________________________________________
   
	strMyTitle = InputBox("Enter the title for this note", "Input Evernote Note Title")
'________________________________________________________________________________________________________________
'   Call powershell script and pass title as a parameter
'________________________________________________________________________________________________________________

	Call Shell("powershell.exe  -ExecutionPolicy Unrestricted -file " & cPWSScript & " -title " & """" & strMyTitle & """")
End Sub
  • When you execute this Outlook macro, you get a user prompt to enter the title of the note - looks like this: 

Outlook macro prompting for Evernote note title

  • In Powershell - if you want something other than the out of the box version of this, you must customize the script in a few places to make it work for you.  Again I'm assuming you know how to use Powershell to do this.  Change the path and file name of the first 2 variables below suited to your location (mine was in a Box subfolder.  The join-path statement concatenates the home directory path to the front of the final name.  In this case that would be "C:\users\USER", plus the Box folder.  Your location will vary - you can eliminate the join-path statement and just put the full path and file name directly in the assignment of the variable.  "$master_evernote_note_template" is the .enex file that you exported that has all your desired formatting and tagging, plus the keywords you are going to use to search and replace.  "$temp_evernote_note_template" is the name of the file we are creating when we copy this master template - this is so we can edit the copy of the file, not the master.  We'll delete this copy when we are done with the script. 

[string]$master_evernote_note_template  = join-path $home ("\box\stuff\master_import_evernote_note.enex")
[string]$temp_evernote_note_template     = join-path $home ("\box\stuff\temp_import_evernote_note.enex")     
    
The 3rd variable you must change is "$evernote_notebook" - this is the notebook that the imported note will be saved to.  
[string]$evernote_notebook              = "instor"      

  • If you want to use the template as-is, you are done.  If not, you can customize Powershell script by changing the "-replace" statements with a different find/replace combo.  Remember you must have unique strings in the export .enex file so it find do the find/replace.
  • To create a note, invoke the macro from Outlook via a button on the menu bar or from the VBE editor  The script runs in seconds, and here's what the resulting note looks like - notice default notebook, tagging, and formatting:
    Output from Outlook Macro with custom title and subheading with date/time stamp

Here's the final Powershell code with comments - enjoy - :mellow: comments encouraged.  I don't know if this is crazy or not...

<#____________________________________________________________________________________________________________________________
    
.DESCRIPTION 
    This script runs creates a new Evernote note by using the import function of the "enscript.exe" module provided as part of the 
    standard Evernote install on Windowws.
    
    You must create a Evernote note template that will be used for the import. You do this by exporting a note that will serve 
    as your template (do this from the Evernote menu).  An example note template is provided as part of the forum post.
    
    The flow of the entire process is as follows:
        Execute Outlook VBA macro to prompt for title of new note
        Outlook VBA takes title as input from user, passes it to a Powershell script as a parameter (this script)
        This script takes the input parameter and does the following:
            Checks for existence of a temp file created during this script, and deletes it if found
            Makes a copy of the master note template to a temp copy of the file so it can edit the temp copy
            Creates a string variable containing the date in the "0800 10 Oct 2017" format
            Opens the temp XML file and uses the Powershell "-replace" command to replace the title and 
                subject keywords, then writes the changes back to disk
            Takes the title passed from the Outlook VBA macro and Runs the "enscript.exe" program to import the temp xml note
                just modifiedand puts it into the specified notebook
            Deletes the temp XML file we created before

    By using an imported note, you don't have to worry about the formatting and tagging.  You can add more fields to the template
    by editing the master template XML and adding more field names than can be updated using the Powershell "-replace" command.
    If you want to do this do this, you'll need to prompt for more inputs (i.e. subject, attendees, etc) on the Outlook VBA part 
    of this automation, and pass more parameters to Powershell script.

    One thing you must do - these are instructions completely borrowed from this post - 
        http://www.thoughtasylum.com/blog/2010/3/14/an-improved-template-system-for-evernote.html, 
        go into the note you exported to the .enex format (edit with Notepad) and remove the data in the
        "<created>" and "<modified> tags, otherwise your note will show the wrong create date. Here's where you do that:
        
        When you export a file as an ENEX file for use as a template, open it up in your favourite text editor (e.g. Windows notepad).  
        Search through the file for two sets of tags "<created>" and "<updated>".  In the template this follows just after the "</content>".  
        Once you have found these tags, delete the tags and any data contained within 
        (e.g. <created>20100310T175541Z</created><updated>20100312T194829Z</updated>).  Once you have done this save and close the file.
     _____________________________________________________________________________________________________________________________
#>
 	param([Parameter(Mandatory=$True,Position=0)][string]$title) 

<# __________________________________________________________________________________________________________________________________________________
   CONFIGURATION VARIABLES - FILE LOCATIONS
  ___________________________________________________________________________________________________________________________________________________________________  #>
   
    [string]$evernote_path          = (split-path (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\evernote.exe').'(default)' -Parent) + "\enscript.exe"    # location of evernote enscript.exe executable 
 	
    [string]$master_evernote_note_template  = join-path $home ("\box\andre_stuff\master_import_evernote_note.enex")         # Location of "master" xml file - make copy of this
    [string]$temp_evernote_note_template    = join-path $home ("\box\andre_stuff\temp_import_evernote_note.enex")           # name of the copy, will edit this one 
    
    [string]$evernote_notebook              = "instor"                                                                      # specify which notebook to store into
<# __________________________________________________________________________________________________________________________________________________
   Check that there is not an old copy of the temp file we are creating hanging around.  If found, delete it
  ___________________________________________________________________________________________________________________________________________________________________  #>
   
    if (Test-Path $temp_evernote_note_template) {                                       # delete the old output file if it exists
        Remove-Item $temp_evernote_note_template
    }

<# __________________________________________________________________________________________________________________________________________________
   Make a copy of the master note template (previously exported) - it is an XML file even though it doesn't have .xml suffix
  ___________________________________________________________________________________________________________________________________________________________________  #>
  
    copy-item $master_evernote_note_template $temp_evernote_note_template
<# __________________________________________________________________________________________________________________________________________________
   Get the current date and time in the "0800 10 Oct 2017" format - we will put this in the note we create as a subheading
  ___________________________________________________________________________________________________________________________________________________________________  #>
 

    $now = get-date -format "HHmm d MMM yyyy"

<# __________________________________________________________________________________________________________________________________________________
  Now use the Powershell "-replace" command to do a find/replace on 2 fields in the the XML file.  After doing a find/replace, write it back out.

  I only have two fields that I'm updating - you could add more if you go into the XML and add a keyword that could be part of the 
  more find/replace operations.
    * The subheading ("subject_template") that will be replaced with the date/time in the "0700 10 Oct 2017" format.
    * The note title - I used the words "title_template" in the XML master file, so I search on this and replace with the title passed from 
        the Outlook portion of this automation.

  I'm retrieving the XML note template as a raw string, and doing a find/replace on text.  
  ___________________________________________________________________________________________________________________________________________________________________  #>
 

   (Get-Content $temp_evernote_note_template -Raw).Replace('subject_template',$now) | Set-Content $temp_evernote_note_template   # add the date-time stamp to the note
   (Get-Content $temp_evernote_note_template -Raw).Replace('title_template',$title) | Set-Content $temp_evernote_note_template   # add a custom title based on input from the text file

<# __________________________________________________________________________________________________________________________________________________
   Now invoke the Evernote enscript.exe executable to import the copy of the master note template and assign it to the notebook specified.
  ___________________________________________________________________________________________________________________________________________________________________  #>
 
   & $evernote_path importNotes /s $temp_evernote_note_template /n $evernote_notebook
<# __________________________________________________________________________________________________________________________________________________
   When all done, delete the copy of the master note template we just edited.
  ___________________________________________________________________________________________________________________________________________________________________  #>
   
   if (Test-Path $temp_evernote_note_template) {                                       # delete the file we just created during cleanup
        Remove-Item $temp_evernote_note_template
   

 

evernote_template_after.JPG

Link to comment
  • Level 5*

Hi.  Outstanding use of VBA,  but - creating a template note and copying it to be repeated on demand is pretty easy,  and there's a keyboard shortcut to create a date/ time stamp. 

I use a text-expander - Phrase Express - to insert a date in various formats (20171031 1600 for example) in my notes with one key combination. 

It's also possible to create a template,  with pauses for user contributions and current dates etc in PE.  http://www.phraseexpress.com/

Link to comment
  • Level 5*
On 2017-10-27 at 3:03 PM, agglomerator said:

The solution was a short Outlook VBA macro that calls a Powershell script

A good solution for customizing a template
I'm using a Mac with scripting via AppleScript and basically the same "replace" process

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...