VBA Project to add a VBA-Syntax-Highlighter to Word
The VBA-Highlighter analyzes the contents of all #paragraphs with the #ParagraphStyle "Code" #periodically and highlights the Syntax of #Keywords, #Datatypes, #Strings and #Comments using #CharacterStyles. Custom Keywords are generally supported, as long as a corresponding #CharacterStyle exists. All #Keywords and #Datatypes are stored in an #ini-file. The identification of a #CharacterStyle is done by the name of a category inside of the #ini-file. Linebreaks and continuation are not supported
The ini-file is structured having a categoryname inside square brackets [ ] followed by a vbCrLf, followed by a list of keywords split by vbCrLf. There must be no duplicates. E.g:
[Keyword]
For
Each
To
Next
[Datatype]
String
Integer
Long
[ManualHighlight]
cKeyDict
Todo
cKeyword
A predefined ini-file is included as VBASyntax.ini
String-literals are determined as any number of Characters between two DoubleQuotes inside a single Paragraph.
Comments are determined as being the following Text after a Single Quote ' or after the Keyword Rem until the end of the paragraph. Single quotes or Rem inside double quotes are considered as being part of a string literal and thus not marked as Comment.
keywords, datatypes and custom keywords are detected by iterating through all words inside a paragraph. KDCs are considered being part of a string literal if they appear inside double quotes. KDCs are considered being part of a comment if they are found after a single quote or Rem.
Paragraphs are determined by iterating through the built in Paragraphs()-Collection of a word Document-Object. Only Paragraphs with the #ParagraphStyle Code are considered.
CharacterStyles for (built in) #Keywords, #Datatypes, #Comments and #Strings are mandatory and can be generated by the program itself. By default Keywords are styled in a dark-blue color, while Datatypes are shown in orange. Strings are lightly-blue colored, Comments green.
Only Paragraphs that are Styled with the "Code"-ParagraphStyle are considered as being relevant. The ParagraphStyle "Code" is mandatory and can be generated by the program itself. By default the Font is set to "Courier New", the FontSize to 11 and the margin between Paragraphs is set to 0. The rest of the style is derived from wdNormal.
A testdriven approach to programming has been chosen. The tests are packed inside the Modules test.bas and UnitTests.bas. The Code is developed using RubberduckVBA.