Parse HTML code and save formatted text to Excel cell
I created a TFS hierarchical style report to show
- User Stories
- Test Cases
The report works well, however it lists the Test Step in HTML format.
When I export the report to Excel through VS, the HTML is not displayed in user friendly format.
I started to google around for solutions, and these are the steps:
- Select the HTML and send to clipboard
- Paste back to Excel with "Paste Special" to preserve the format
- When paste multiple lines of text, excel puts them in multiple rows, and the vba collects all the rows and puts them back to one row
These are the protocol code:
Private Sub Convert_Click()
Dim rng As Range
Set rng = ActiveSheet.Range("G3")
Worksheet_Change rng, ActiveSheetEnd Sub
Private Sub Worksheet_Change(ByVal Target As Range, ByVal sht As Worksheet)
Dim objData As DataObject ' Set a reference to MS Forms 2.0
Dim sHTML As String
Dim sSelAdd As String
Dim i As Long
Dim txt As String
Application.EnableEvents = False
If Target.Cells.Count = 1 Then
Set objData = New DataObjectsHTML = "<html><DIV>Test Steps</DIV><DIV>Test Results</DIV>" & Target.Text & "</html>"
objData.SetText sHTMLobjData.PutInClipboardsht.PasteSpecial Format:="Unicode Text"
For i = 1 To Selection.Cells.Count
txt = txt & Selection(i) & Chr(10)
Next
Selection.ClearContents Selection(1) = txt
End If
Application.EnableEvents = True
End Sub
Comments
Post a Comment