Search code examples
htmlvb6vbscripthardcode

Hard-coding HTML into VB6 code


Background: we have an application that generates reports from HTML (that may or may not have inline scripting). The HTML source is normally stored as a blob in the database.

There is now the need to hard-code a particular report into the application (i.e. so that it is not database dependent). I first tried it the brute force way (cutting and pasting the whole report into a const string and appending a whole lot of & vbNewLine & _ to it; that didn't work because there appears to be a limit to the number of & _ that can be used. I thought of compressing everything into (more or less) a single line, but not only would that hurt readability, it also wouldn't work for the inline scripting.

Something just occurred to me while writing this: I could open the file (containing the HTML I want to hardcode) programmatically and write the file's contents into a string. I'll give that a go now...

Can anyone suggest a better/more elegant way of doing this?


Solution

  • I ended up embedding the HTML file into a resource (res) file and loading it up from there using LoadResData. I asked another question relating to the loading up of HTML files from res files (and got a pretty good answer too). Note that another option could be to embed the HTML (or any other text file) as a Custom Resource; this way you'll be able to reference the resource by name (i.e. the name of the Custom Resource) when using LoadResData rather than a number (which may not mean too much to someone who comes along and tries to understand your code). Note also that if you want to load the HTML into a string (as I do), you'll need to call StrConv on the result returned by LoadResData (LoadResData returns an array of bytes).