【问题标题】:Copy text from an IE page and paste it back to a text box in the same page using excel VBA从 IE 页面复制文本并使用 excel VBA 将其粘贴回同一页面中的文本框
【发布时间】:2020-03-09 04:51:22
【问题描述】:

使用 Excel VBA,我正在尝试创建一个宏。

目的是让它在 Internet Explorer 中导航以完成一项任务。

我正在尝试从网页中的红色标记框(每个都是单独的条目)复制文本并将其粘贴到同一页面上方的输入框中。

比如说,08:28 应该复制到上面的文本框中。

同样,每个条目都应复制到它们上方的相应输入框中,如下所示:

请教我如何使用 Excel VBA 查找、复制和粘贴数据。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请注意,由于这不是免费的代码编写服务,因此您有必要展示到目前为止您尝试过的内容以及遇到的问题或错误(通过展示您的代码),或者至少展示您的研究内容和您所做的努力制成。否则,它只是要求我们为您完成所有工作。阅读How to Ask 可能会帮助您改进您的问题。
  • 能否贴出相关网页的html资源,让我们知道您使用的是哪些html元素,帮助您解决问题。你可以创建一个Minimal, Complete, and Verifiable example
  • 感谢您让我知道这件事。我尝试使用以下代码访问该页面:
  • Sub Automate_IE_Load_Page() Dim i As Long URL As String Dim IE As Object Dim objElement As Object Dim objCollection As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True URL = ">" IE.Navigate URL Application.StatusBar = URL & " 正在加载。请稍候..." Do While IE.ReadyState = 4: DoEvents: Loop 'Do While Do until IE.ReadyState = 4: DoEvents : Loop 'Do until Application.StatusBar = URL & "Loaded" Set IE = Nothing Set objElement = Nothing Set objCollection = Nothing End Sub
  • 啊啊啊为什么代码是这样粘贴的:(

标签: excel vba internet-explorer webautomation ie-automation


【解决方案1】:

由于您没有发布html资源,所以我们不知道您使用的是哪个HTML元素,建议您参考以下通用方法复制并设置值。

一般情况下,我们可以使用 getElementById()、getElementsByClassName()、getElementsByName()、getElementsByTagName() strong>、querySelector() 和 querySelectorAll() 先查找网页元素,然后使用 innerText 或 innerHtml 属性来设置值。

从截图来看,您似乎正在创建一个时间表,所以,我想它是一个 html 表格元素,并且您想将值从一行复制到目标行(可能使用输入文本或不使用输入文本),请参考以下示例代码:

网页资源:

<table >
    <tr>
        <td>11 MON</td>
        <td>12 TUE</td>
        <td>13 WED</td>
        <td>14 THU</td>
        <td>15 FRI</td>
        <td>16 SAT</td>
        <td>17 SUN</td>
    </tr>
    <tr>
        <td id="label2_1" class="class2_1"><input type="text" value="" /></td>
        <td id="label2_2" class="class2_2"><input type="text" value="" /></td>
        <td id="label2_3" name="wedtime2_3"><input type="text" value="" /></td>
        <td id="label2_4"></td>
        <td id="label2_5" class="class2_5"></td>
        <td id="label2_6" name="wedtime"></td>
        <td><input id="txt7" type="text" value="" /></td>
    </tr>
    <tr>
        <td id="label3_1">08:28</td>
        <td id="label3_2" class="class3_2">02:46</td>
        <td id="label3_3" name="wedtime3_3">03:46</td>
        <td id="label3_4">04:46</td>
        <td id="label3_5" class="class3_5">05:46</td>
        <td id="label3_6" name="wedtime">06:46</td>
        <td>07:46</td>
    </tr>
</table>

然后,使用以下 VBA 脚本复制数据:

Sub Test()
    Dim IE As Object

    Dim startDateText As Object, endDateText As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "<web page url>"

        While IE.ReadyState <> 4
            DoEvents
        Wend

        'using getElementById() method to get the td tag, then using getElementsByTagName() method to get the input text box.
        'using the innertext property to set the input text box value, using the innerHtml property to get the value.
        IE.Document.getElementById("label2_1").getElementsByTagName("input")(0).innerText = IE.Document.getElementById("label3_1").innerHtml
        
        'using getElementsByClassName method to get the elements.
        IE.Document.getElementsByClassName("class2_2")(0).getElementsByTagName("input")(0).innerText = IE.Document.getElementsByClassName("class3_2")(0).innerHtml
        
        'using getElementsByName method to get the elements.
        IE.Document.getElementsByName("wedtime2_3")(0).getElementsByTagName("input")(0).innerText = IE.Document.getElementsByName("wedtime3_3")(0).innerHtml
       
       'Using innerHtml property to set the value.
        'using getElementsByClassName method to get the elements.
        IE.Document.getElementById("label2_4").innerHtml = IE.Document.getElementById("label3_4").innerHtml
        
        'using getElementsByClassName method to get the elements.
        IE.Document.getElementsByClassName("class2_5")(0).innerHtml = IE.Document.getElementsByClassName("class3_5")(0).innerHtml
        
        'using getElementsByName method to get the elements.
        IE.Document.getElementsByName("wedtime")(0).innerHtml = IE.Document.getElementsByName("wedtime")(1).innerHtml
       
    End With
    Set IE = Nothing
End Sub

此外,您还可以使用以下脚本循环遍历td标签并设置值

        'find all of the tr.           
        Dim TableRows As Object
        Set TableRows = IE.Document.getElementsByTagName("table")(0).getElementsByTagName("tr")
        'Debug.Print TableRows.Length
        
        Dim targetrow As Object
        Dim datarow As Object
        Set targetrow = TableRows(1).getElementsByTagName("td")
        Set datarow = TableRows(2).getElementsByTagName("td")
        
        Dim i As Integer
        Dim text As Object
        'loop through the td and set value.
        For i = 0 To targetrow.Length - 1
            Set text = targetrow(i).getElementsByTagName("input")(0)
            If Not text Is Nothing Then
                text.innerText = datarow(i).innerHtml
            Else
                targetrow(i).innerHtml = datarow(i).innerHtml
            End If
        Next i

结果如下:

有关使用这些方法的更多详细信息,请参阅common VBA method or properties。

如果还是不能解决您的问题,请贴出相关网页的html资源,最好让我们知道您使用的是哪些HTML元素,帮助您解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多