2012-10-10 40 views
0

我試圖使用下面的代碼刪除html標籤,但它什麼也沒做。我試過再次運行它,但沒有用。有人可以請告知可能是什麼問題。使用html標籤附加文件的快照。有關信息,數據來自MS Access和MS Access鏈接到Sharepoint列表。使用正則表達式,但它不會刪除excel表中的html標籤

enter image description here

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 
+0

正則表達式操縱HTML只會讀到瘋狂。 –

+0

我認爲[Jeff Atwood](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html)說得最好。 – psubsee2003

+0

我不想用正則表達式。我手頭有一個問題(即來自Sharepoint導入數據的html標籤),我會採用任何方法來擺脫這些標籤。有什麼建議嗎? – joiner

回答

1

您設置的模式屬性多次,它只會保留(&amp;)分配的最後一個值。

您需要使用3個正則表達式("<[^>]+>" => """&nbsp;" => " ""&amp;" => "&"),或匹配所有的輸入("(&amp;)|(&nbsp;)|(<[^>]+>)" => ""),以真正做到這一個表情。

+0

謝謝大家的關注。 (「(&)|( )|(<[^>)+>)」)工作。感謝Joaquim R. – joiner