Here’s some code that I’m pretty proud of, its a nifty piece of code that allows to store any kind of data in a simple textbased container. It uses a custom Base64 encoding to store integers, floats, strings, booleans or blobs in a text block and adds some CRC checks and even some repair capabilities.
First question that probably comes to mind; what the heck do you want to use something like this for? Well, I actually had two cases where I used it;
- When a recurring task in Outlook is finished, a new task is automatically created. Yet any custom properties that task has, will not be copied to the new task. Hence that data is lost. As the text body is moving over, storing the data in the text body is a workaround to pass the data on. But I didn’t like it to be editable by the user, so some sort of INI like structure wasn’t ok, it had to use some sort of encoding en needed to have some verification on tampering.
- Working for a customer that uses very strict firewall rules turned my life into email hell. Word or Excel documents would pass, but not much more. Content of zip files was being checked, and encrypted zips we’re not allowed. By adding the binary files (just MS Project .MPP files in this case) in encoded format to a word document, the files could pass. As everything is in VBA, on the receiving end, the data and the logic required to decode the data we’re all in the same Word document (download the example below).
NOTE: Sending files through firewalls this way is a way to circumvent security measures, please be carefull when using this. If you choose to use it, you will bear sole responsibilty for it. Use at your own risk!
[ad name=”468×60 Banner”]
Features of the class
- Text based startheader
- Text based endheader
- merging of multiple datablocks into 1
- includes basic CRC checks to verify data integrity and even make some repairs
- survives emailing with lines being broken up
- Store as many values as you want
- Values can also be entire files, directly being loaded from/saved to disk (date/timestamps will be preserved)
- custom Base64 en/decoding with CRC checks
How it works
The code consists of two classes, the main class does the encoding and contains a collection of items (bools, strings, blobs, etc.) that are included in the data block. The items in the collection are based upon the second class.
When working with the class, you can just provide the full text of the container (for example an email), specific start and end markers are used to find the datablock inside the text. When adding or changing data, only the block between the markers is updated, the remainder is left unchanged. Here’s an example of what it looks like;
In case of an email, this would be the email text, below you see the two “DO NOT EDIT …” lines, which are the markers between which the data is located.
Any text below the data block is also preserved. In the block you can see that the last column and the last row contain the CRC data, just by the looks of it.
External references used by the project;
- This module uses the FileSystemObject, and hence it requires a reference to “Microsoft Scripting Runtime” in VBA menu Tools|References…|dialog
- This module relies on 2 modules by Chip Pearson for time calculations and file manipulation (of date/time stamps). The required modules are: modGetSetFileTimes, modTimeConversionFunctions.
[ad name=”468×60 Banner”]
Example and downloads
The download (downloaded 2003 times) contains a Word document which includes all VBA code (including the other code required; Chip Pearsons, and some generic stuff found elswhere on my site). Try the example (its the firewall passing application), if you save the file included in it, you’ll get a nice picture of me!