views:

620

answers:

3

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?

+1  A: 

If that's the way you have to go then think of using the html string as a template with placeholders like @@var1@@ in the string which you can then replace with the actual dynamic values rather than trying to generate the final output inline, should be much easier to debug issues.

Sijin
+2  A: 

You could read each line of the file and append it as you go. Alternatively, you could use a StringBuilder class. You would need to either write this class or copy it from somewhere like this. Doing things this way has potential to improve performance as well.

Dim oSB as CStringBuilder
Dim sHtml as String

Set oSB = new CStringBuilder
With oSB
  Call .Append("Some HTML here")
  Call .Append("Some more HTML here")
  ' etc...
  sHtml = .ToString()
End With

Set oSB = Nothing
Matthew Cole
+1  A: 

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).

jpoh