Mayur Alaspure
Mayur Alaspure

Reputation: 69

VBA to fetch attribute values from HTML

I am trying to create macro where it will fetch the data from particular attribute, and simply paste on excel, I tried it using getElementsById, but its not working for me.

I have link:
https://secure.bmw.com/_common/silo/pages/form-tabs/controller_opt.jsp?locale=XQ_en&usercountry=IN&tracking_usercountry=IN&tracking_locale=XQ_en&transfer=f23_info_req_lp

Just view source it and you will find the attribute name "lead context"

<input type="hidden" name="reg_source" value="OPK">
<input type="hidden" name="lead_context" value="F45CV ROI OPK">
<input type="hidden" name="subscription_lead_context" value="F45CV ROI OPK">
<input type="hidden" name="cam_source_code" value="1-1521452141">
<input type="hidden" name="offer_code" value="1524521452">

So basically I want value "OPK", "F45CV ROI OPK", "1-1521452141" and "1524521452" on excel sheet.

Can anyone help me with simple code, Sorry for as i am new in VBA.

Thanks for your time :)

Regards, Mayur

Upvotes: 0

Views: 1940

Answers (1)

user4039065
user4039065

Reputation:

One method is grab all <input> elements and cycle through them to collect what you need. The variable assignment I've used requires Tools, References, Microsoft HTML Object Library but you could just call them Objects if you preferred.

Dim eNPT As MSHTML.IHTMLElement, ecNPTs As MSHTML.IHTMLElementCollection
Set ecNPTs = ie.document.getElementsByTagName("input")
Range("A1:A5").ClearContents
For Each eNPT In ecNPTs
    Select Case LCase(eNPT.getAttribute("name"))
        Case "reg_source"
            Range("A1") = eNPT.Value
        Case "lead_context"
            Range("A2") = eNPT.Value
        Case "subscription_lead_context"
            Range("A3") = eNPT.Value
        Case "cam_source_code"
            Range("A4") = eNPT.Value
        Case "offer_code"
            Range("A5") = eNPT.Value
        End Select
        If Application.CountA(Range("A1:A5")) = 5 Then Exit For
Next eNPT
Set ecNPTs = Nothing: Set eNPT = Nothing

Upvotes: 1

Related Questions