简体   繁体   English

从 Excel VBA 中的字符串中删除 HTML 标签

[英]Remove HTML tags from string in Excel VBA

I want to remove all HTML tags from a string in Excel VBA.我想从 Excel VBA 中的字符串中删除所有 HTML 标记。

For example:例如:

before_text = "text1 <br> text2 <a href = 'www.data.com' id = 'data'>text3</a> text4"

after_text = RemoveTags(before_text)

Result:结果:

after_text = "text1  text2 text3 text4"
vbscript.regexp

Code:代码:

Function RemoveHTML(text As String) As String
    Dim regexObject As Object
    Set regexObject = CreateObject("vbscript.regexp")

    With regexObject
        .Pattern = "<!*[^<>]*>"    'html tags and comments
        .Global = True
        .IgnoreCase = True
        .MultiLine = True
    End With

    RemoveHTML = regexObject.Replace(text, "")
End Function

Building on @zhihar's reply, to make this strip all HTML from the selected cell you can iterate through the selection.以@zhihar 的回复为基础,要从所选单元格中删除所有 HTML,您可以遍历选择。

Function RemoveHTML(text As String) As String
    Dim regexObject As Object
    Set regexObject = CreateObject("vbscript.regexp")

    With regexObject
        .Pattern = "<!*[^<>]*>"    'html tags and comments
        .Global = True
        .IgnoreCase = True
        .MultiLine = True
    End With

    RemoveHTML = regexObject.Replace(text, "")
End Function


Sub StripHtmlSelected()
    For Each Cell In Selection
        If Not Cell.HasFormula Then
            Cell.Value = RemoveHTML(Cell.Value)
        End If
    Next Cell
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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