【问题标题】:Importing data using VBA in excel from webpage having 2 tables with same ID在 excel 中使用 VBA 从具有 2 个具有相同 ID 的表的网页导入数据
【发布时间】:2014-10-08 03:54:16
【问题描述】:

首先,我要感谢 Siddharth Rout 和 IAmDranged 先生解决了与使用 VBA 从安全网站导入 excel 数据相关的问题,正如我之前的问题所讨论的那样。 Runtime error 438 while importing data in excel from secured website using VBAUnable to import data in excel from another website using VB code。该代码现在运行良好,但还有一个问题,即网页,我试图从中导入数据,有两个具有相同 ID“报告表”的表。使用 VBA 代码只能从第一个表中复制数据。 VBA 中需要进行哪些修改才能同时从两个表(相同 ID)表中复制数据。 VBA代码和目标网页源代码再次贴在下面。

Sub GetTable()

     Dim ieApp As InternetExplorer
     Dim ieDoc As Object
     Dim ieTable As Object
     Dim clip As DataObject

     'create a new instance of ie
     Set ieApp = New InternetExplorer

     'you don’t need this, but it’s good for debugging
     ieApp.Visible = True

     'assume we’re not logged in and just go directly to the login page
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/LoginAction.do?hmode=loginPage"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     Set ieDoc = ieApp.Document

     'fill in the login form – View Source from your browser to get the control names
     With ieDoc
    .getElementById("userId").setAttribute "value", "rlbdgs"
    .getElementById("userPassword").setAttribute "value", "123"

    '~~> This will select the 2nd radio button as it is `0` based
    .getElementsByName("userType")(1).Checked = True

    .getElementById("hmode").Click
     End With
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     'now that we’re in, go to the page we want
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&lobby=BSL&type=B&startDate=&endDate=&traction=ELEC"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     'get the table based on the table’s id
      Set ieDoc = ieApp.Document
      Set ieTable = ieDoc.getElementById("report-table")    
     'copy the tables html to the clipboard and paste to the sheet
     If Not ieTable Is Nothing Then
         Set clip = New DataObject
         clip.SetText "<html>" & ieTable.outerHTML & "</html>"
         clip.PutInClipboard
         Sheet1.Select
         Sheet1.Range("A1").Select
         Sheet1.PasteSpecial "Unicode Text"
     End If

     'close 'er up
     ieApp.Quit
     Set ieApp = Nothing

 End Sub 

网页来源

<html>
<head>
<title>CREW BOOKED ON TA</title>

<link href="../styles/reportStyle.css" rel="stylesheet" type="text/css" />

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

 <script type="text/javascript">
  function DoNav(theUrl)
  {
    //alert(theUrl);
    document.location.href = theUrl;
  }
  </script>
</head>
<body>
<table id="report-table">
    <!-- Table header -->
        <thead>
            <tr>
                <th scope="col" class="date" style="border:0px;" colspan="10">Print Date Time: <span>14-08-2014 13:30</span></th>
            </tr>
            <tr>
                <th scope="col"  class="report-cris" style="text-align:center;">CRIS</th><th scope="col" class="report-heading" style="text-align:center;" colspan="8">VIGILENCE CONTROL DEVICE (VCD) IN LOCO NOT WORKING(SIGN OFF THROUGH KIOSK)(LOCO SHED WISE)(LAST 24 HOURS)<th scope="col"  class="report-cris" style="text-align:center;">CMS</th>
            </tr>
            <tr style="border:none;">
                <th colspan="9" style="border-right:none;">
                                <span class="report-button" onclick="javascript:history.back();">BACK</span>
                                <span class="report-button" onclick="javascript:window.print();">PRINT</span>
                </th>
                <th style="border-left:none;text-align:right;"></th>
            </tr>
        </table>
    <table id="report-table">
    <thead>
        <tr style="border:none;" align="center">
                <th>S.No.</th>
                <th>ID</th>
                <th>NAME</th>
                <th>SIGNOFF DATE</th>
                <th>FROM</th>
                <th>TO</th>             
                <th>LOCO NO.</th>
                <th>BASE SHED</th>
                <th>RAILWAY</th>
            </tr>
        </thead>
        <tbody>

                    <tr>
                        <td>1</td>                              
                        <td>BINA1482</td>
                        <td >RAKESH KUMAR BAJPAI</td>
                        <td>14-08-2014 11:07</td>
                        <td >BINA</td>
                        <td>ET  </td>                       
                        <td>23551   </td>
                        <td>BRC</td>
                        <td>WR  </td>                       
                    </tr>                   

            </tbody>
</table>
* If duration for this report is last 24 hours or from and to date is same, then only last VCD reporting of the loco will be shown.
</body>
</html>

请提出解决方案。

【问题讨论】:

    标签: html vba excel import


    【解决方案1】:

    在我看来,在同一页面上有两个具有相同 id 的元素的 HTML 看起来不合适,但这可以解释为什么 ieDoc.all.Item("report-table") 返回一个集合。我想下面应该工作 - 恢复到 Set ieTable = ieDoc.all.Item("report-table"),然后遍历返回的集合项。

    由于服务器当前向下看,因此无法对此进行测试

     Set ieTable = ieDoc.all.Item("report-table")
    
     'copy the tables html to the clipboard and paste to the sheet
     If Not ieTable Is Nothing Then
        oHTML = ""
        For i = 0 To ieTable.Length - 1
            oHTML = oHTML & ieTable.Item(i).outerHTML
        Next i
        Set clip = New DataObject
        clip.SetText "<html>" & oHTML & "</html>"
        clip.PutInClipboard
        Sheet1.Select
        Sheet1.Range("A1").Select
        Sheet1.PasteSpecial "Unicode Text"
     End If
    

    【讨论】:

      【解决方案2】:

      Set ieTable = ieDoc.all.Item("report-table") 上方的行应该返回所有“报告表”元素的集合。尝试遍历它们。像这样的:

      For Each ieTable In ieDoc.all.Item("report-table")
          'do stuff
      Next ieTable
      

      此外,从 IE 11 开始,将不支持 .all。 MS 建议使用getElementById

      【讨论】:

      • 对不起,我错误地发布了未更正的代码。 ieDoc.all.Item 已按照 Siddharth 先生的建议替换为 getElementById。现在也在上面提到的代码中进行编辑。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多