如何使用 Application.SendKeys 解决破坏 InternetExplorer 对象的问题

How do I get around breaking InternetExplorer Object with Application.SendKeys

我以为我正在走向成功。我想要做的是在 IE 中加载 URL,输入一些数据,然后 运行 一份报告。

为此,我创建了一个 InternetExplorer 对象,使用该对象打开可见性,并导航到我的 URL。

进入该页面后,我必须提供一些数据让报表生成器执行它需要执行的操作,然后按 Enter 键。一旦我到达这一点,我需要对不同的 URL 进行一些导航,直到我到达报告页面。为此,我尝试 return 使用我的 InternetExplore 对象 (IE) 进行导航,但每次我使用 IE.anything 时都会弹出 462 错误。

我的问题有两个:

  1. 对象一开始很好,我不得不假设是 Application.Send 或 Application.Wait 的使用以某种方式破坏了我的对象,为什么?
  2. 我做错了什么?
  3. 关于如何解决这个问题的建议?

    Dim text As String
    Dim IE
    Set IE = CreateObject("InternetExplorer.Application")
    '
    IE.Visible = True
    IE.Navigate "http://~~~~~~~~~.net/reports/views/result/reportResult.faces"
    '
    text = "somedata"
    .
    Application.Wait (Now + TimeValue("00:00:02"))
        Application.SendKeys (text), True
    '
    Application.Wait (Now + TimeValue("00:00:02"))
        Application.SendKeys vbCrLf, True
    '   
    Application.Wait (Now + TimeValue("00:00:02"))
        IE.Navigate "http://~~~~~~~~~.net/reports/views/myMrs/myFavorites.faces"
    

最终产品与我的目标完全不同。感谢那些提供意见的人,尤其是让我开始关注元素的 Sorceri。

下面是完成的代码段(在此之后我还有更多的事情要做,但这让我到达了我需要去的地方)。

Private Sub OpenReport()
Dim text As String
Dim x As Integer
Dim IE
Set IE = New InternetExplorerMedium
text = "somecode"

'Load link
IE.Visible = True
IE.Navigate "http://~~~~~~~~~/reports/views/result/reportResult.faces"

'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
    DoEvents
Loop

'fill in the pcode
IE.Document.getelementbyid("code").Value = text

'Click Submit
IE.Document.getElementsByClassName("btn btn-lg btn-primary btn-block")(0).Click

'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
    DoEvents
Loop

'Click on Favorites
IE.Document.getElementsByClassName("myMRSFavoritesLink")(0).Click

'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
    DoEvents
Loop

'Click on report
IE.Document.getElementsByName("myFavoritesForm:treeTable_tableId:1:infoButton101644806j_id__v_19")(0).Click

'Wait for page to load
Do While IE.Busy Or IE.ReadyState <> 4
    DoEvents
Loop

Click on execute report
IE.Document.getElementsByName("setParametersForm:startReportButton")(0).Click

End Sub