错误代码91对象变量或未设置带块变量

时间:2019-07-06 17:52:02

标签: html excel vba web-scraping internet-explorer-11

我正在尝试执行网络自动化,这涉及登录并在电子表格中的特定文本上搜索特定文本到网页上的特定框。 我在第二步遇到错误,将值粘贴到网页的搜索框中。

代码中的错误行:

EnterName.value = Firstname

我已尝试登录,但此步骤成功。但是,第二步没有成功。

Public IE as Object
Public Entername as Object
Public HTMLdoc as Object

Sub Pink()
'logging in to webpage

   username1 = Activeworkbook.worksheets("Sheet1").range("B2").value
   password1 = Activeworkbook.worksheets("Sheet1").range("B3").value

' Opening IE explorer

Set IE =  New InternetExplorerMedium

With IE
  . Visible = true
  . navigate "website"
  while .Busy or .readyState<>4:DoEvents: Wend
End With

'Actual login
Dim Login as object
Dim password as Object

Set Login = HTML.getElementsByName("Username")(0)
Set password = HTML.getElementsByName("Password")(0)

Login.Value = username1
password.Value = password1

HTMLdoc.forms(0).submit

With  IE

while .Busy or .readyState<>4:DoEvents: Wend
End With

call Step2

End sub

************************************************************************
Sub Step2()

Firstname = Range("A8").value

lastName = Range("C8").value 

set  EnterName =  HTMLdoc.getElementsByName("PRIMARY")(0)

EnterName.value =  Firstname

End sub 

********************************************************************

IE11 Inspect Elements

<input name ="PRIMARY" tableindex="0" class="dijitReset dijitInputInner"
id  ="indium_view_form_ValidationTextbox_0" aria-invalid="false" aria-
required="true" type="text"  maxlenght="1000" autocomplete="off" data
dojo-attach-point="textbox,focusnode" value=""></input>

我希望将range(“ A8”)中的值粘贴到IE网页上。但是我得到一个错误91。对象变量或未设置块变量

2 个答案:

答案 0 :(得分:0)

您永远不会将HTMLDoc实例化为New HTMLDocument。您需要这样做。因此,我希望您的代码在提到的行之前出错。

您还引用了HTML变量,但看不到已声明或实例化的变量。我希望这两个变量应该只是一个,并作为参数传递给第二个子变量。

HTML.getElementsByName("Username")(0)

使用所有模块顶部的Option Explicit检查这些错误。


我还将HTMLDoc和工作表作为参数传递给第二个子控件,而不是使用公共变量。


请注意,由于您未完全限定范围,因此您正在使用隐式Activesheet引用,因此容易出错。

Range("A8").Value

上面没有明确的工作表参考。下面的示例使用显式引用:

ThisWorkbook.Worksheets("Sheet1").Range("A8").Value

答案 1 :(得分:0)

我建议您检查Object variable not set (Error 91):创建对象变量有两个步骤。首先,必须声明对象变量。然后,您必须使用Set语句为对象变量分配有效的引用。

因此,在创建Htmldoc对象之后,我们需要为其分配一个有效的引用。

我尝试使用以下代码创建示例,您可以参考它。

Public IE As Object
Public Entername As Object
Public Htmldoc As Object

Sub Test()

username1 = ThisWorkbook.Sheets("Main Page").Range("B2").Value
password1 = ThisWorkbook.Sheets("Main Page").Range("B3").Value

Dim Rank As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate ("http://localhost:54382/HtmlPage47.html")
Do
    If IE.readyState = 4 Then
        Exit Do
    Else
    End If
Loop

Set Htmldoc = IE.document

Dim Login As Object
Dim password As Object

Set Login = Htmldoc.getElementsByName("Username")(0)
Set password = Htmldoc.getElementsByName("Password")(0)

Login.Value = username1
password.Value = password1

Htmldoc.forms(0).submit
With IE

While .Busy Or .readyState <> 4: DoEvents: Wend
End With

Call Step2

End Sub


Sub Step2()

Firstname = ThisWorkbook.Sheets("Main Page").Range("A8").Value

Set Entername = Htmldoc.getElementsByName("PRIMARY")(0)

Entername.Value = Firstname

End Sub

网页中的代码:

<form>
    <div>
        <input name="Username" id="Text1" type="text" /><br />
        <input name="Password" id="Text1" type="text" /><br />
        <input id="Submit1" type="submit" value="submit" /><br />
        <input name="PRIMARY" tableindex="0" class="dijitReset dijitInputInner" id="indium_view_form_ValidationTextbox_0" aria-invalid="false" aria-required="true" type="text" maxlenght="1000" autocomplete="off" datadojo-attach-point="textbox,focusnode" value=""></input>
    </div>
</form>
相关问题