简体   繁体   中英

VBA Split HTML String

I have a field in a SQL-Server 2008R2 table which contains values like this (all lines in one field):

<div><font face=""Times New Roman"" size=3 color=black>MyCustomer Name Ltd.</font></div>

<div><font face=""Times New Roman"" size=3 color=black>MyCustomer Adress</font></div>

<div><font face=""Times New Roman"" size=3 color=black>MyCustomer Zip-code MyCustomer City</font></div>

I am working in Access 2010 and have to Export this Content into an Excel sheet with 3 columns: Name, Adress, Zip+City

I tried with

Split(rs("Myfield"), "<div><font face=""Times New Roman"" size=3 color=black>")

but it does not work.

Any idea how to split this string? Thanks Michael

You are splitting it on the wrong delimiter...

Logic

  1. Replace <div><font face=""Times New Roman"" size=3 color=black> in the main string with blank
  2. Split on </font></div>

Is this what you are trying?

Sub Sample()
    Dim s As String

    's = rs("Myfield")
    s = "<div><font face=""""Times New Roman"""" size=3 color=black>MyCustomer Name Ltd.</font></div>" & _
        "<div><font face=""""Times New Roman"""" size=3 color=black>MyCustomer Adress</font></div>" & _
        "<div><font face=""""Times New Roman"""" size=3 color=black>MyCustomer Zip-code MyCustomer City</font></div>"

    'Debug.Print s

    s = Replace(s, "<div><font face=""""Times New Roman"""" size=3 color=black>", "")

    Debug.Print Split(s, "</font></div>")(0) '<~~ MyCustomer Name Ltd.
    Debug.Print Split(s, "</font></div>")(1) '<~~ MyCustomer Adress
    Debug.Print Split(s, "</font></div>")(2) '<~~ MyCustomer Zip-code MyCustomer City
End Sub

Sorry Siddharth my incorrect use of comments.

Meanwhile I managed my Problem of variable number of lines by this

Do While Not rs.EOF
m = rs("MyField")
m = Replace(m, "<div><font face=""Times New Roman"" size=3 color=black>", "")
s() = Split(m, "</font></div>")

For i = LBound(s()) To UBound(s()) 
    Debug.Print s(i)

Next i

rs.MoveNext

Loop

Thanks again yr. reply! Michael

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM