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



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

代码中的错误行:

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>

我期望在IE网页上粘贴范围(" A8"(中的值。但是我遇到了一个错误91。对象变量或块变量未设置

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

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

HTML.getElementsByName("Username")(0)

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


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


并注意您正在使用隐式Activesheet参考文献,因为您不满意您的范围,因此这是错误的。

Range("A8").Value

以上没有明确的工作表参考。以下示例使用明确的参考:

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

我建议您检查对象变量未设置(错误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>

最新更新