Public Function GetData(ByVal prodID As String) As String
Dim htm As Object
Dim theURL, desc As String
theURL = "http://www.monoprice.com/products/product.asp?p_id=" + prodID
Set htm = CreateObject("htmlFile")
With CreateObject("msxml2.xmlhttp")
.Open "GET", theURL, False
.send
htm.body.innerHTML = .responseText
End With
desc = htm.getelementbyid("product-name").innerHTML
GetData = StripHTML(desc)
End Function
Public Function GetPrice(ByVal prodID As String) As String
Dim htm
Dim hElem, divElem As MSHTML.HTMLGenericElement
Dim theURL, desc As String
Dim row As Range
Dim index As Integer
Application.Volatile
theURL = "http://www.monoprice.com/products/product.asp?p_id=" + prodID
Set htm = CreateObject("htmlFile")
With CreateObject("msxml2.xmlhttp")
.Open "GET", theURL, False
.send
htm.body.innerHTML = .responseText
End With
'Loop through all the rows and sum items with the same PID
'row.Column(2) = PID
'row.Column(3) = Quantity purchased
prodCount = 0
For Each row In ActiveSheet.UsedRange.Rows
If row.Columns(2) = prodID And row.Columns(2) <> "PID" Then
prodCount = prodCount + row.Columns(3).Value
End If
Next row
'loop through table tags
For Each hElem In htm.getElementsByTagName("table")
If hElem.getAttribute("width") = "260" Then
Set divHTML = CreateObject("htmlFile")
divHTML.body.innerHTML = hElem.innerHTML
index = 0
For Each divElem In divHTML.getElementsByTagName("div")
index = index + 1
If prodCount = 1 And index = 4 Then
GetPrice = divElem.innerHTML
Exit For
ElseIf prodCount >= 2 And prodCount <= 9 And index = 6 Then
GetPrice = divElem.innerHTML
Exit For
ElseIf prodCount >= 10 And prodCount <= 19 And index = 8 Then
GetPrice = divElem.innerHTML
Exit For
ElseIf prodCount >= 20 And prodCount <= 49 And index = 10 Then
GetPrice = divElem.innerHTML
Exit For
ElseIf prodCount >= 50 And nerdCount = 12 Then
GetPrice = divElem.innerHTML
Exit For
End If
Next divElem
End If
Next hElem
End Function
Function StripHTML(sInput As String) As String
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
Dim sOut As String
With RegEx
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "<[^>]+>|\?" 'Regular Expression for HTML Tags.
sOut = RegEx.Replace(sInput, "")
.Pattern = "[^A-Za-z0-9 \|\/\.\-]" ' & Chr(39) & "]"
sOut = RegEx.Replace(sOut, "")
End With
StripHTML = sOut
Set RegEx = Nothing
End Function