使用Excel VBA按ID在IE中导航

时间:2017-06-21 20:18:58

标签: html excel-vba internet-explorer vba excel

问题:尝试将信息从Excel复制/粘贴到网页中,然后按下按钮。最终需要将其打印到文件夹中。我尝试了下面的代码,但我完全不确定它为什么不起作用。谷歌没有解决。

网站:https://www.easymapmaker.com/advanced

尝试过的代码:

    Sub MapMacro()

    AddressGrid = Range("A1").Value

    Set IE = CreateObject("InternetExplorer.Application")
    WebSite = "https://www.easymapmaker.com/advanced"
    With IE
      .Visible = True
      .navigate WebSite

    Do While IE.Busy Or IE.readyState <> 4
        DoEvents
    Loop
    On Error Resume Next 'This is here in case fields cant be found.

    Set Address = IE.Document.getElementsByID("sourceData")
    Address.Value = AddressGrid


    Set Element = IE.Document.getElementsByID("optionButton")
    Element.Click

    Do While IE.Busy Or IE.readyState <> 4
        DoEvents
    Loop
  End With

End Sub

1 个答案:

答案 0 :(得分:0)

为了澄清,我通过更改行来实现这一点:

Set Address = IE.Document.getElementsByID("sourceData")
Address.Value = AddressGrid

为:

IE.Document.getElementByID("sourceData").Value = AddressGrid

现在,由于AddressGrid只引用一个单元格但你想要一个范围,比如说“A1:D4”你可以试试这个方法:

Dim clipTXT As MSForms.DataObject
Range("A1:D4").Copy

Set clipTXT = New MSForms.DataObject
    clipTXT.GetFromClipboard
IE.Document.getElementByID("sourceData").Value = clipTXT.GetTxt