|
2#
楼主 |
发表于 2011-10-13 22:33:41
|
只看该作者
我给出我的解决方法:- 'How can we find the current page and the total page values from the string "Page 4 of 15"?
- Option Explicit
- Dim strText
- strText = "Page 4 of 15"
- 'msgbox Mid(strText, 6, 1)
- 'Msgbox Right(strText, 2)
- Dim strCurrPage, strTotPage
- GetCurrTotPage strText, strCurrPage, strTotPage
- msgbox "Current Page is " & strCurrpage & "!" & vbCrLf & _
- "Total Page is " & strTotPage & "!"
- Public Function GetCurrTotPage(ByVal strText, ByRef strCurrPage, ByRef strTotPage)
- 'Use Regular Expression
- Dim rePattern, reObj
- rePattern = "\d+\s*of\s*\d+"
- Set reObj = New RegExp
- reObj.Pattern = rePattern
- reObj.Global = False
- reObj.IgnoreCase = True
- 'msgbox reObj.Test(strText)
- If reObj.Test(strText) Then
- Dim oCollMatches
- Set oCollMatches = reObj.Execute(strText)
- If oCollMatches.Count > 0 Then
- Dim strMatch, strMatchValue
- For Each strMatch in oCollMatches
- strMatchValue = strMatch.Value
- strCurrPage = Trim(Mid(strMatchValue, 1, InStr(strMatchValue, "of") - 1))
- strTotPage = Trim(Mid(strMatchValue, InStr(strMatchValue, "of") + 2))
- Next
- End If
- GetCurrTotPage = True
- Else
- msgbox "Please pass the valid string into function!"
- End If
- End Function
复制代码 |
|