简体   繁体   中英

Pulling Data from an embedded web page with VBA Excel

I am trying to use VBA in Excel to access data in a webpage that is embedded in a webpage. I know how to do this if the table is on a non-embedded page. I also know how to navigate to this product's page using VBA. I cannot just navigate to the embedded page because there is a product id look up that converts a part number to an id and i don't have access to that database.

Here is the link to the page: http://support.automation.siemens.com/WW/view/en/7224052

I would have posed a picture of the element for clarity but I don't have 10 rep points...

The table I need to get information from is the "Product Life Cycle" table.

I can see the correct url in a property called src under the corresponding item if I save the page as an HTMLDocument in VBA using the following code:

For Each cell In Selection
    link = "http://support.automation.siemens.com/US/llisapi.dll?func=cslib.csinfo&lang=en&objid=" & cell & "&caller=view"
    ie.navigate link
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE
    Dim doc As HTMLDocument

Is there a way to index this table with VBA or will I have to contact the company and attempt to get access to the product ID so I can navigate to the page directly?

Regarding my comment below, here is the code that a recorded macro yeilds:

ActiveCell.FormulaR1C1 = _
    "http://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&lang=en&objid=6ES7194-1AA01-0XA0&caller=view"
Range("F9").Select
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW" _
    , Destination:=Range("$F$9"))
    .FieldNames = True
    .RowNumbers = False

I know where to find the string: URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW , but I don't know how to save it to a variable.

Not sure I exactly understand your question, but here is some code that will get the source code behind the table of interest. You can extract the data of interest using functions like "instr" and "mid"

' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")

With ie
    .Visible = True
    .navigate "http://support.automation.siemens.com/WW/view/en/7224052"
    .Top = 50
    .Left = 530
    .Height = 400
    .Width = 400

Do Until Not ie.Busy And ie.ReadyState = 4
    DoEvents
Loop

End With

' Assign the source code behind the page to a variable
my_var = ie.document.frames(3).document.DocumentElement.innerhtml

' Extract the url for the "Product life cycle" table
pos_1 = InStr(1, my_var, "product life cycle", vbTextCompare)
pos_2 = InStr(pos_1, my_var, "/WW/llisapi", vbTextCompare)
pos_3 = InStr(pos_2, my_var, """><", vbTextCompare)
pos_4 = InStr(pos_3, my_var, """/>", vbTextCompare)
table_url = Mid(my_var, pos_2, pos_3 - pos_2)
table_url = Replace(table_url, "amp;", "", 1, -1, vbTextCompare)
table_url = "http://support.automation.siemens.com" & table_url

' navigate to the table url
ie.navigate table_url

Do Until Not ie.Busy And ie.ReadyState = 4
    DoEvents
Loop

' assign the source code for this page to a variable and extract the desired information
my_var2 = ie.document.body.innerhtml
pos_1 = InStr(1, my_var2, "ET 200X, basic modules,", vbTextCompare)

' close ie
ie.Quit

I have had problems getting ron's code to work, I think becuase IE doesn't work easily with frames. Below is some code that will extract some of the data from the table you have mentioned, it so far doesn't handle the diagrams.

Sub FrameStrip()


    Dim oFrames As Object
    Dim tdelements As Object
    Dim tdElement As Object
    Dim oFrame As MSHTML.HTMLFrameElement
    Dim oElement As Object
    Dim sString As String
    Dim myVar As Variant
    Dim sLinks() As String
    Dim i As Integer
    Dim bfound As Boolean
    Dim url As String
    Dim oIE As InternetExplorer


    Set oIE = New InternetExplorer

    url = "http://support.automation.siemens.com/WW/view/en/7224052"

    'Set address for use with relative source names
    myVar = Split(url, "/")
    sString = myVar(0) & "//" & myVar(2)

    oIE.navigate url
    oIE.Visible = True

    Do Until (oIE.readyState = 4 And Not oIE.Busy)
        DoEvents
    Loop

    Set oFrames = oIE.document.getElementsByTagName("frame")
    ReDim sLinks(oFrames.Length)

    'Get the source locations for each frame
    i = 0
    For Each oFrame In oFrames

       sLinks(i) = sString & (oFrame.getAttribute("src"))

       i = i + 1
    Next oFrame

    'Go through each frame to find the table
    i = 0
    bfound = False
    Do While i < UBound(sLinks) And bfound = False
        oIE.navigate sLinks(i)
        Do Until (oIE.readyState = 4 And Not oIE.Busy)
            DoEvents
        Loop

        Set oElement = oIE.document.getElementById("produktangaben")
        bfound = IsSet(oElement)
        i = i + 1
    Loop

    Set tdelements = oElement.getElementsByTagName("td")
    'Display information about table
    sString = ""
    For Each tdElement In tdelements
       Debug.Print tdElement.innerText
       sString = sString & tdElement.innerText
    Next tdElement


End Sub

Function IsSet(ByRef oElement As Object) As Boolean

    Dim tdelements As Object
    Dim bSet As Boolean

    bSet = True

    On Error GoTo ErrorSet
    Set tdelements = oElement.getElementsByTagName("td")
    On Error GoTo 0


Cleanup:

    On Error Resume Next
    Set tdelements = Nothing
    On Error GoTo 0

    IsSet = bSet

Exit Function

ErrorSet:
    bSet = False
    GoTo Cleanup:
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM