Adding VB/VBA Support

May 25, 2016 at 5:48 AM
So I primarily program in VBA (and am starting to dabble in I was amazed this doesnt support either (or at least I will be the first to admit I am not a pro programmer and have an extremely minimal understanding of C#

After looking in the source code I realized that this addin uses another program to actually handle the syntax highlighting called "highlight.exe". The addin calls this via CLI and passes arguments to it to specify what language and style to use. Its worth mention that this program has a standalone version that can be used manually outside the addin. I highly recommend downloading it and installing as we will need 2x files from it *more on this later

What further surprises me is that there are some mentions of VB in the source code. For example in the "CodeForm.cs" (in the "NoteHighLightForm" folder/node), there is a case statement that refers to VB/VBNET.

So I realized that the addin uses highlight.exe to syntax highlight and then performs some sort of conversion on its results as html to convert it into text with formatting that OneNote will accept (because why in the world would a program like OneNote be able to handle various text/formatting /sarcasm). The conversion from html to OneNote acceptable text doesnt care what language we use as the highlight basis.

Highlight.exe has configuration files for each language (.lang) and configuration files for each editor (.theme). These are essentially text files that store the parameters of the rules for the languages highlighting and what colors to use for them.

This led me to 2 ideas:
  1. Long term it should be relatively easy for someone to add the button for the VB/VBA language to the ribbon, update the required files (*this is the area I am least qualified for), and add the required language file...then repackage the installer.
  2. Short term I was able to replace another language with the requisite .lang content for VB and use it in the addin. I did this in OneNote 2016 x64 but the steps should basically be the same in x32 or ON2013
Here are the steps I took:
  • Install this addin using the x32 or x64 bit installer based on your version
  • Download and install highlight.exe Here *as of this writing version 3.29 (note 3.29 > 3.4)
  • Go into the standalone folder for highlight.
  • Inside is a folder called "langDefs"
  • In "langDefs" is a file called "vb.lang".
  • Copy this to the "highlight\langDefs" subfolder of the NoteHighLight2013 install directory.
  • Go back to the standalone folder for highlight
  • Inside is a folder called "themes"
  • Copy the desired theme file to the "themes" folder (in my case I wanted the most up to date copy of the Visual Studio theme, "edit-msvs2008.theme") of the "highlight\themes" subfolder of the NoteHighLight2013 install directory.
*We have to select a language to replace. I do not think it matters which but I chose to replace the Python language with the VB language. This essentially means I would click the Python button on the ribbon to convert to VB syntax highlights, but would have no option to convert to Python syntax.
  • Rename the .lang file you intend to replace to something like *.lang.old (where * is whatever the name was)
  • Make a copy of the vb.lang file in that same folder and then rename it to the language we are replacing. ex: "copy vb.lang" would be renamed to "python.lang" if you are replacing the python syntax with VB syntax.
  • open the "new" "python.lang" file and change the description at the top from ="Visual Basic" to ="Pyhton" (or whatever the proper description is for the language we replaced).
  • close OneNote and reopen
  • copy some VB/VBA code from the source
  • click the button on the NoteHighLight tab in OneNote for the language you replaced with the VB syntax. Paste the code into the dialog and hit ok.
This should follow the VB language rules and the coloring you selected.

As a note, the purpose of getting the language file from the latest release of the standalone highlight.exe is it fixes an issue caused in the VB syntax when "\" is used. Using an older version of the vb.lang resulted in "\" specifically causing syntax highlighting issues. The language file for VB as of at least 3.29 resolves this.

highlight.exe handles many more languages then this addin provides buttons for. I would presume this process could be used for just about any language that highlight.exe supports. This is at least a short term work around to it not being officially added to the addin.

I will likely take a shot at adding a new button to the addin and having it access the VB syntax highlights properly (like it does with currently included languages) and recompiling. If and when I can do this will depend largely on finding free time and making heads or tails of C#. Hope this helps others.