Hardcode HTML to VB6 code

Background: we have an application that generates reports from HTML (which may or may not have embedded scripts). The HTML source is usually stored as a block of data in a database.

Now you need to hard-code the specific report in the application (i.e. so that it doesn't depend on the database). I first tried it with brute force (cutting and pasting the whole report into a const string and adding a lot to it & vbNewLine & _

, it didn't work because there is a limit on the number & _

that can be used. I was thinking about compressing everything into (more or less) one line but not only will it impair readability, it will not work for inline scripts either.

Something came to my mind while writing this: I could programmatically create a file (containing the HTML that I want to hardcode) and write the contents of the file to a string. I'll give it now ...

Can anyone suggest a better / more elegant way to do this?

+1


source to share


3 answers


I ended up embedding the HTML file in the resource file (res) and loading it there using LoadResData. I asked another question related to loading HTML files from res files (and got a pretty good answer too). Note that another option would be to embed HTML (or any other text file) as a custom resource; this way you will be able to refer to the resource by name (i.e. the custom resource name) when using LoadResData, not a number (which might not mean too much to someone who comes in and tries to understand your code). Also note that if you want to load HTML into a string (like me) you will need to call StrConv on the result returned by LoadResData (LoadResData returns a byte array).



+1


source


You can read each line of the file and add it as you go. Alternatively, you can use the StringBuilder class. You will need to either write this class or copy it somewhere like this . This has the potential to improve productivity.



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
+2


source


If that's what you need, consider using an html string as a template with placeholders like @@ var1 @@ in the string, which you can then replace with actual dynamic values ​​instead of trying to generate the final result inline, it should be much easier to debug problems.

+1


source







All Articles