VBA RegEx on HTML

我已经做了一些阅读,明白,我不应该使用正则expression式的HTML。

然而,如果有人能够提供一些见解,我可能会更好地从一系列单元格中删除简单的HTML标记(粗体,斜体和下划线),我将不胜感激。 现在,我的macros不断返回1004的运行时错误:“删除字符类失败的方法”。

在这一点上,我会采取任何简单的解决scheme,这个问题..任何意见?

我的代码:

For Each c1 In textRange.Cells strInput = UCase(c1.text) With objRegEx .Global = True .Pattern = "<\/?\w.?>" If .test(strInput) Then Set RegMC = .Execute(strInput) For Each RegM In RegMC c1.Characters(RegM.FirstIndex + 1, RegM.Length).Delete Next End If End With Next c1 

我猜如果你的单元格包含多个标签,那么最后的删除将失败,如果标签是在string的末尾。 由于您修改了string,但没有重新计算正则expression式,因此索引可能会大于更新后的string。

为什么不使用Replace

  Replace(c1.text, "</b>", "") 

对于每一件你想摆脱的东西都要这样做。

略有不同的做法:

 Function HtmlToText(html As String) As String Static el As New MSHTML.HTMLDocument Static div As Object If div Is Nothing Then Set el = New MSHTML.HTMLDocument Set div = el.createElement("div") el.appendChild div Debug.Print "created" End If div.innerHTML = html HtmlToText = div.innerText End Function 

需要引用“Microsoft HTML Object Library”