Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 变量已定义和设置时VBA中出现错误91_Excel_Vba_Internet Explorer_Automation_Runtime Error - Fatal编程技术网

Excel 变量已定义和设置时VBA中出现错误91

Excel 变量已定义和设置时VBA中出现错误91,excel,vba,internet-explorer,automation,runtime-error,Excel,Vba,Internet Explorer,Automation,Runtime Error,我是一名编码新手,这是我在StackOverflow上的第一篇文章。 我正试图在工作中自动化一项重复的任务 因此,需要先登录,然后删除一个文本字段(例如,“0000”应该在这里),然后将Excel中的另一个值粘贴到另一个字段中,单击“搜索”并刮取一些数据 HTMLInput.Value = "0000000" 在这里,我得到了 运行时错误91:对象变量或未设置 我真的不明白为什么会这样。我认为每个变量都有定义 或者我在登录后遗漏了什么,比如“HTMLInput”应该发生的其他事情 Optio

我是一名编码新手,这是我在StackOverflow上的第一篇文章。 我正试图在工作中自动化一项重复的任务

因此,需要先登录,然后删除一个文本字段(例如,“0000”应该在这里),然后将Excel中的另一个值粘贴到另一个字段中,单击“搜索”并刮取一些数据

 HTMLInput.Value = "0000000"
在这里,我得到了

运行时错误91:对象变量或未设置

我真的不明白为什么会这样。我认为每个变量都有定义

或者我在登录后遗漏了什么,比如“HTMLInput”应该发生的其他事情

Option Explicit

Sub LogIn()

    Dim IE As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLInput As MSHTML.IHTMLElement

    IE.Visible = True

    IE.Navigate Sheet1.Range("B2").Text

    Do While IE.ReadyState <> READYSTATE_COMPLETE
    DoEvents
    Loop

    Set HTMLDoc = IE.Document

    'Setting Username to a specific value
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_UserName")
    HTMLInput.Value = Sheet1.Range("B3")

    'Setting Password to a specific value
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_Password")
    HTMLInput.Value = Sheet1.Range("B4")

    'Click to login
    Set HTMLInput = HTMLDoc.getElementById("ctl00_WebPartManager1_gwpLogin1_Login1_LoginButton")

    HTMLInput.Click

    Set HTMLInput = HTMLDoc.getElementById("ctl00_SearchSection_ObjectSearch_txtEMail")

    HTMLInput.Value = "0000000"

    Set HTMLInput = HTMLDoc.getElementById("ctl00_SearchSection_ObjectSearch_txtCustomerID")
    HTMLInput.Value = Sheet1.Range("A10")

End Sub
选项显式
子登录()
Dim IE作为新的SHDocVw.InternetExplorer
将HTMLDoc设置为MSHTML.HTMLDocument
将HTMLInput设置为MSHTML.IHTMLElement
可见=真实
即导航表1.范围(“B2”).文本
在IE.ReadyState ReadyState\u完成时执行此操作
多芬特
环
设置HTMLDoc=IE.Document
'将用户名设置为特定值
设置HTMLInput=HTMLDoc.getElementById(“ctl00\u WebPartManager1\u gwpLogin1\u Login1\u用户名”)
HTMLInput.Value=Sheet1.Range(“B3”)
'将密码设置为特定值
设置HTMLInput=HTMLDoc.getElementById(“ctl00\u WebPartManager1\u gwpLogin1\u登录1\u密码”)
HTMLInput.Value=Sheet1.Range(“B4”)
'单击以登录
设置HTMLInput=HTMLDoc.getElementById(“ctl00\u Web部件管理器1\u gwpLogin1\u登录按钮”)
HTMLInput。单击
设置HTMLInput=HTMLDoc.getElementById(“ctl00\u SearchSection\u ObjectSearch\u txtEMail”)
HTMLInput.Value=“0000000”
设置HTMLInput=HTMLDoc.getElementById(“ctl00\u SearchSection\u ObjectSearch\u txtCustomerID”)
HTMLInput.Value=Sheet1.Range(“A10”)
端接头

在尝试将
值设置为
0000000
之前,您的
Set HTMLInput=…
语句似乎返回
NULL
。您不能设置nothingTry的值来检查网页中的输入元素是否具有“ctl00\u SearchSection\u ObjectSearch\u txtEMail”的确切id。检查它的拼写。如果不正确,请尝试更正。嗨,Martin,我必须将该文本字段的值设置为空的原因是,在搜索特定项(“A10”)之前,所有文本字段都必须为空。必须是empy的文本字段在我每次登录时都已经包含一些文本。@Deepak MSFT HTML id(ctl00\u SearchSection\u ObjectSearch\u txtEMail)只包含一个特定的文本字段。可能还有一点需要说明的是,在代码单击登录后,页面会重定向到我需要的平台。因此,我猜测在登录和下一步开始后是否缺少一些代码行。或者我应该做一个新的潜艇?