VBA截取html内容的小函数

不用正则表达式,简单截取两个字符串中间的内容。比如:

strhtml="<td>这里是内容</td>"

想获取“这里是内容”,就这样用这个函数:

MidBetween(0,strhtml,”<td>”,”</td>”)

其中nCursor是开始查找的位置,如果想从头开始,就是0。函数很简单,但是类似的功能经常要自己写,如果不是VBA已经停止支持,估计微软会把它加进来。

Function MidBetween(nCursor, strText, strStart, strEnd)

    Dim nStart, nEnd
    
    If InStr(nCursor, strText, strStart) = 0 Or InStr(nCursor, strText, strEnd) = 0 Then
        MidBetween = ""
    Else
        nStart = InStr(nCursor, strText, strStart) + Len(strStart)
        nEnd = InStr(nStart, strText, strEnd)
        MidBetween = Mid(strText, nStart, nEnd - nStart)
    End If
    
End Function

以上。

懒得每次重写的老狼

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注