真?使用IE命令在VBA中导致此错误的原因是什么?

时间:2017-01-05 19:19:24

标签: excel vba excel-vba internet-explorer

我在尝试将变量传递到此IE命令时遇到问题:

IE.Document.All( Var1 )。值= ThisWorkbook.Sheets(“Sheet1”)。范围(“B5”)

如果我输入“Response123”而不是Var1例如 - 一切正常。 IE打开,文本框中填充了我的Excel工作表上范围“B5”的值。但是,当我用 Var1 IE崩溃替换它时,我得到一个运行时错误:

“'2147023170(800706be)':自动化错误远程过程调用失败”

我发现这个链接解决了这个问题,但解决方案对我来说没有意义。我试图像它建议的那样重新声明对象,但它不会编译。 https://support.microsoft.com/en-us/kb/230385

这是我的代码:

Public Sub Populate()

Dim Var1 As String
Dim Var2 As String
Dim Var3 As String

Dim IE As Object

Call Data1 (Var1, Var2, Var3)

DoEvents

'FillInternetForm
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

  Set IE = CreateObject("InternetExplorer.Application")
  IE.Navigate "https://website.com"
  IE.Visible = True
  While IE.busy
    DoEvents  'wait until IE is done loading page.
  Wend

  IE.Document.All(Var1).Value = ThisWorkbook.Sheets("Sheet1").Range("B5")
  IE.Document.All(Var2).Value = ThisWorkbook.Sheets("Sheet1").Range("B6")
  IE.Document.All(Var3).Value = ThisWorkbook.Sheets("Sheet1").Range("B7")

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub


Public Sub Data1 (ByRef Var1 As String, ByRef Var2 As String, ByRef Var3 As String)

Var1 = "Response123"
Var2 = "Response456"
Var3 = "Response789"

End Sub

我试图调试并且它被卡住了:

IE.Document.All(Var1).Value = ThisWorkbook.Sheets(“Sheet1”)。范围(“B5”)

然而,当我将光标悬停在Var1上时,它显示为“Response123”,这就是我想要的。如果我删除Var1并只是手动输入“Response123”它按预期工作。我真的很茫然。提前感谢您对此的任何帮助。

1 个答案:

答案 0 :(得分:0)

您的代码的简化版本适合我(见下文)。除非您分享有关确切用例的更多详细信息,否则我们不太可能提供更多...

Public Sub Populate()

    Dim Var1 As String
    Dim IE As Object

    Data1 Var1

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "https://www.google.com"
    IE.Visible = True
    While IE.busy
        DoEvents
    Wend
    Application.Wait (Now + TimeSerial(0, 0, 3))

    IE.Document.All(Var1).Value = "test"

End Sub

Public Sub Data1(ByRef Var1 As String)
    Var1 = "q"
End Sub