0

我尝试使用下面的代码删除 html 标签,但它没有做任何事情。我已经尝试再次运行它但没有用。有人可以请教可能是什么问题。使用 html 标签附加文件的快照。有关信息,数据来自 MS Access 和指向 Sharepoint 列表的 MS Access 链接。

在此处输入图像描述

Sub Import_AccessData()
 Dim strtKeyMsgRange As Range
 Dim KeyMsgRange As Range
 Dim KeyMsgRangeCell As Range
 Dim endKeyMsgRangeCell As Range

Set strtKeyMsgRange = Range("B2")
Set endKeyMsgRange = Range("AC13")

Set KeyMsgRange = Range(strtKeyMsgRange, endKeyMsgRange)

For Each KeyMsgRangeCell In KeyMsgRange
   a = StripHTML(KeyMsgRangeCell)
   KeyMsgRangeCell.Value = a
Next KeyMsgRangeCell

End Sub


Public Function StripHTML(cell As Range) As String
     Dim RegEx As Object
     Set RegEx = CreateObject("vbscript.regexp")

     Dim sInput As String
     Dim sOut As String


     sInput = cell.Value

     With RegEx
   .Global = True
   .IgnoreCase = True
   .MultiLine = True
   .Pattern = "<[^>]+>" 'Regular Expression for HTML Tags.
   .Pattern = "&nbsp;"
   .Pattern = "&amp;"
 End With

 sOut = RegEx.Replace(sInput, "")
 StripHTML = sOut
 Set RegEx = Nothing
End Function
4

1 回答 1

1

您正在多次设置 Pattern 属性,它只会保留最后分配的值 ( &amp;)。

您需要使用 3 个正则表达式 ( "<[^>]+>" => "", "&nbsp;" => " ", "&amp;" => "&") 或一个匹配所有输入 ( "(&amp;)|(&nbsp;)|(<[^>]+>)" => "") 的表达式来实际执行此操作。

于 2012-10-10T19:47:06.927 回答