Consulting

Results 1 to 11 of 11

Thread: Solved: Displaying Cells with HTML-code properly

  1. #1
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location

    Solved: Displaying Cells with HTML-code properly

    I have a sheet where two columns have Cells with HTML Code in it.

    How can you make sure that the tags aren't showed?

    <html><body><font color="#000080"><b>Name&lt;ecslst&gt;, 10/10/2007: </b></font> <br><br>Description</body></html>
    Thanks in advance for the help.

    EDIT: The tags in all the cells are not always the same.
    Last edited by Marcke; 11-18-2008 at 09:38 AM.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You should avoid the select but this will show you how to put the text into the cell without the tags I think:
    [VBA]
    Range("F13").Select
    ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
    [/VBA]

    Stripping html can get pretty involved so I think it would be better if you can figure out a way to enter the text without the tags to begin with if possible.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Hi, thanks for your suggestion, I'll try it tomorrow since I don't have a testing environment here.

    I'm quite new to vba; why should you avoid the select? For performance issues?

    Next to that: I'm extracting some values through an API to Excel, and the extract is in html.
    I'll try to do the following:
    1)Extracting
    2)Copying
    3)Pasting Special

    If anyone has other suggestions, feel free to add them.

  4. #4
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    I tried the following code with the fields that allready had HTML tags in them, but VBA gives me an error. Does anyone have an idea why?

    [VBA]'Select Start Cell
    shPrintout.Range("G4:G9999").Select
    shPrintout.Range("G4:G9999").Copy
    shPrintout.Range("G4:G9999").Select
    ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False[/VBA]

  5. #5
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Some more information that might help:

    The following code is within a loop that extracts the HTML fields from an Object I create through an API

    [VBA]
    shPrintout.Cells(iRow, iCol) = ExtractedObject.field(shPrintout.Cells(jRow, jCol))
    [/VBA]

  6. #6
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Hi all,

    I've let this task rest for a while, but will continue on it as of today.

    Does anyone have any suggestions?

    Thanks in advance for your help,

    Wannes

  7. #7
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Anybody want to give it another try?

  8. #8
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Hi all,

    So my problem still is not solved.

    However, I found the following link but there's still one issue that I cannot solve.
    http://www.dailydoseofexcel.com/arch...l-in-cells-ii/

    The problem is that when a textfield has <br> it's spread over multiple cells, while I'd like a new line within the cell.

    However, I don't understand much of the code, so don't know where to begin;

    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim objData As DataObject
    Dim sHTML As String
    Dim sSelAdd As String

    Application.EnableEvents = False

    If Target.Cells.Count = 1 Then
    If LCase(Left(Target.Text, 6)) = "<html>" Then
    Set objData = New DataObject

    sHTML = Target.Text

    sHTML = Replace(sHTML, "<html>", "<html><style>br{mso-data-placement:same-cell;}</style>")

    objData.SetText sHTML
    objData.PutInClipboard

    sSelAdd = Selection.Address
    Target.Select
    Me.PasteSpecial "Unicode Text"
    Me.Range(sSelAdd).Select

    End If
    End If
    Application.EnableEvents = True
    End Sub
    [/vba]

  9. #9
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Quote Originally Posted by lucas
    You should avoid the select but this will show you how to put the text into the cell without the tags I think:
    [vba]
    Range("F13").Select
    ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
    [/vba]

    Stripping html can get pretty involved so I think it would be better if you can figure out a way to enter the text without the tags to begin with if possible.
    When I try this I get an error with the Format parameter of pastespecial. I've included a test file in appendix

  10. #10
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi Marcke,

    Well if the tags are written properly e.g: < tag>... some stuff...</end tag> then brute force should work... <g>...</g>
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  11. #11
    VBAX Regular
    Joined
    Nov 2008
    Posts
    41
    Location
    Thanks for the help rbrhodes, it works indeed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •