There are several ways to use MXHTMLWriter but here I'll turn it "inside out" by explicitly raising events to it via IVBSAXContentHandler instead of letting other parts of MSXML raise the events. This is a very basic example showing how to do that to write HTML, and in this case the demo involves simple reporting.
Depending on your purpose you might want the results in different ways. Here I show how to get file output, String output, and Byte array output (since for that we can get UTF-8 or other character encodings).
It should be plenty speedy enough for most purposes:
Here is the crux of the demo:
Code:
Private Sub Report(ByRef Dest As Variant, Optional ByVal Encoding As String = "ASCII")
'Dest: Can be an instance of an IStream implementation or a String.
'
'Encoding: Can be "UTF-8" or "Windows-1252" or "UTF-16" etc. as desired.
' Always ignored for String output which is always UTF-16
' ("Unicode").
Const REPORT_TITLE As String = "January 2009 Sales"
Const CSS_STYLES As String = vbNewLine _
& "*{font:normal normal normal 8pt Arial;}" & vbNewLine _
& "th,td{border:1px solid black;}" & vbNewLine _
& "th{background-color:royalblue;color:white;font-weight:bold;}" & vbNewLine _
& "td{background-color:white;color:green;}" & vbNewLine _
& "table,th,td{border-collapse:collapse;}" & vbNewLine _
& ".SH{color:red;}"
Dim Attrs As MSXML2.SAXAttributes60
Dim Handler As MSXML2.IVBSAXContentHandler
Dim Writer As MSXML2.MXHTMLWriter60
Dim FieldsUB As Long
Dim Fields() As ADODB.Field
Dim Col As Long
Dim Row As Long
Dim LatitudeField As Long
Dim Value As Variant
Set Attrs = New MSXML2.SAXAttributes60
Set Writer = New MSXML2.MXHTMLWriter60
Set Handler = Writer
With Writer
.disableOutputEscaping = False
.indent = True
.Encoding = "ASCII"
.byteOrderMark = True 'Has no effect for 8-bit encodings or any String output.
.output = Dest 'Can be an IStream implementation, or a String value to set
'the output type to String.
End With
With RS
.MoveFirst
FieldsUB = .Fields.Count - 1
ReDim Fields(FieldsUB)
For Col = 0 To FieldsUB
Set Fields(Col) = .Fields(Col)
If Fields(Col).Name = "Latitude" Then LatitudeField = Col
Next
End With
With Handler
.startDocument
.startElement "", "", "HTML", Attrs
.startElement "", "", "HEAD", Attrs 'Auto-emits a META tag for encoding.
Attrs.addAttribute "", "", "name", "", "generator"
Attrs.addAttribute "", "", "content", "", App.CompanyName _
& " " & App.EXEName _
& " " & CStr(App.Major) _
& "." & CStr(App.Minor)
.startElement "", "", "META", Attrs
Attrs.Clear
.endElement "", "", "META"
.startElement "", "", "TITLE", Attrs
.characters REPORT_TITLE
.endElement "", "", "TITLE"
Attrs.addAttribute "", "", "type", "", "text/css"
.startElement "", "", "STYLE", Attrs
Attrs.Clear
.characters CSS_STYLES
.endElement "", "", "STYLE"
.endElement "", "", "HEAD"
.startElement "", "", "BODY", Attrs
.startElement "", "", "TABLE", Attrs
.startElement "", "", "TR", Attrs
For Col = 0 To FieldsUB
.startElement "", "", "TH", Attrs
.characters Replace$(Fields(Col).Name, "_", " ")
.endElement "", "", "TH"
Next
.endElement "", "", "TR"
Do Until RS.EOF
'Hightlight rows for Southern Hemisphere:
If Fields(LatitudeField).Value < 0 Then
Attrs.addAttribute "", "", "class", "", "SH"
Else
Attrs.Clear
End If
.startElement "", "", "TR", Attrs
For Col = 0 To FieldsUB
.startElement "", "", "TD", Attrs
Value = Fields(Col).Value
If Not IsNull(Value) Then .characters CStr(Value)
.endElement "", "", "TD"
Next
.endElement "", "", "TR"
RS.MoveNext
Loop
.endElement "", "", "TABLE"
.endElement "", "", "BODY"
.endElement "", "", "HTML"
.endDocument
End With
With Writer
.Flush
If VarType(Dest) = vbString Then
Dest = .output 'Fetch String output.
End If
End With
End Sub
The attachment contains some raw data, which is why it is so large.
MSXML 6.0 has been part of Windows since Vista. You might still be able to download a redist version for XP SP2 or maybe SP3 from Microsoft.
Windows 2000 Service Pack 4, Windows Server 2003, Windows Server 2003 Service Pack 1, Windows XP Service Pack 1, Windows XP Service Pack 2
• To install MSXML 6.0 you must have Microsoft Windows® Installer 3.1 (MSI 3.1) or higher on your computer. If you are running Microsoft Windows Server 2003 Service Pack 1 (SP1) you already have the correct version. For other operating systems you may need to upgrade. Please see the Windows Installer 3.1 knowledge base article for details.
Works fine here but I am using VB6 SP6. Maybe you aren't up to date? I am compiling on Windows 10 1670 so if you are trying to use XP or something that could be part of the problem too.
But you can always just delete the icon or replace it by another. That one has true-color plus alpha images, so you could strip it down to plain 24-bit or 8-bit images.