使用vba将Enter(〜)键发送到外部IE对象

时间:2017-04-05 14:09:57

标签: html vba excel-vba excel-formula excel

我遇到了以下任务:从excel文件中我将数据(对于某个列发送到我发送单元格值的工作表中)发送到预定义的特定IE对象。

使用宏将数据发送到textarea字段(通过ID:id1识别到网站上)的网站上。对于添加到网站的textarea字段中的每个单元格值/数据,输入键/命令需要自动完成/附加,因为这将生成一些空输入字段,以自动完成到网站上(代码如下所示:)

我正在努力使用一种方法,在插入数据后自动将ENTER键发送到网站上的textarea

代码更新..

Sub adddata()

    Dim objIE As Object
    Dim objTR As Object
    Dim i, j, counter As Integer
    Dim lastRow As Long
    counter = 1

    Set objIE = GetIeByTitle("https://exampletest.com", True, True)

    Dim lastRow2 As Integer
    lastRow2 = Workbooks(path1).Worksheets("Test").Range("A" & Rows.Count).End(xlUp).Row

    Workbooks(path1).Worksheets("Test").Activate

    contor = 1

    'First of all the unhidden files have to be take from the target excel file
    'Selecting the unhidden lines from the excel file
    For j = 1 To lastRow2
        If Rows(j).EntireRow.Hidden = False Then
            Workbooks(path1).Worksheets("Test").Range("A" & j & ":Z" & j).Select
            Selection.Copy
            ThisWorkbook.Worksheets("Test2").Range("A" & counter).PasteSpecial
            counter = counter + 1
        End If
    Next j

    'look into the new excel file containing just the unhidden lines
    'Afterwards a look up through new excel file cells
    For i = 2 To counter
        objIE.document.getelementbyid("id1").Value = Worksheets("Test2").Range("C" & i).Value ' taking the value from the cell and adding it on the text area field
        objIE.document.getelementbyid("id1").SetFocus
        Application.SendKeys "~" ' sending the enter key
        Application.Wait (7)     ' add the delay of 7 seconds

        '2nd field
        objIE.document.getelementbyid("id2").Value = Worksheets("Test2").Range("D" & i).Value

        '3rd field
        objIE.document.getelementbyid("id3").Value = Worksheets("Test2").Range("E" & i).Value

    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

看起来你错过了一个结束括号:

Set objIE = GetIeByTitle("https://exampletest.com", True, True ***)***

尝试在代字号周围加上大括号:

    Application.SendKeys "{~}"

编辑:

听起来可能有与文本框更改相关的代码;也许试试:

objIE.document.getelementbyid("id1").fireevent ("onchange")