Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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与IE上的小部件接触_Excel_Vba - Fatal编程技术网

Excel 通过VBA与IE上的小部件接触

Excel 通过VBA与IE上的小部件接触,excel,vba,Excel,Vba,我以前没有真正尝试过这方面的工作,所以我不知道我现在在做什么。我对html的知识有限,所以不确定我是否做得对。基本上,我的目标是通过宏打开InternetExplorer,更改一些基于id的元素,然后单击网站上的submit按钮来显示数据。然后我需要继续下一步的工作 正如您从代码中看到的,我试图通过html代码中的id号与IE上的小部件进行交互 Sub Automate_IE_Enter_Data() 'This will load a webpage in IE Dim i As Lo

我以前没有真正尝试过这方面的工作,所以我不知道我现在在做什么。我对html的知识有限,所以不确定我是否做得对。基本上,我的目标是通过宏打开InternetExplorer,更改一些基于id的元素,然后单击网站上的submit按钮来显示数据。然后我需要继续下一步的工作

正如您从代码中看到的,我试图通过html代码中的id号与IE上的小部件进行交互

Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object

    Dim objbutton As Object


    'Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")

    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True

    'Define URL
    URL = "http://cfpsg1/plant/Reports/ScrapReport.aspx"

    'Navigate to URL
    IE.Navigate URL

    ' Statusbar let's user know website is loading
    Application.StatusBar = URL & " is loading. Please wait..."

    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop

    'Webpage Loaded
    Application.StatusBar = URL & " Loaded"

    IE.Document.getelementbyid("1stGroupBy").Value = "3"

    'Find & Fill Out Input Box

    IE.Document.getelementbyid("PageContent_uxStartDate").Value = "06/21/2019"
    IE.Document.getelementbyid("PageContent_uxEndDate").Value = "06/21/2019"

    Set objbutton = IE.Document.getelementbyid("PageContent_btnQuery")
    objbutton.Focus
    objbutton.Click


    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing

End Sub

首先,
网页弹出,但是除了一个

错误消息“对象“IWebBrowser 2”的方法“Document”失败


显示在
IE.Document.getelementbyid(“1stGroupBy”).Value=“3”
行上。

您试图与下拉列表交互,因此希望使用如下语法

IE.Document.querySelector("[value='3']").Selected = True
你也可以使用

IE.Document.querySelector("#1stGroupBy").SelectedIndex = 2   'change to appropriate index
错误
“对象'IWebBrowser 2'的方法'Document'失败”
可能是由于完整性级别造成的

您可以通过将
完整性
级别更改为中等来尝试下面提到的代码

Dim IE As InternetExplorer

Set IE = New InternetExplorerMedium

IE.Visible = True

URL = "http://cfpsg1/plant/Reports/ScrapReport.aspx"
IE.Navigate URL

Do While IE.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop

Dim IE作为InternetExplorer
设置IE=新的InternetExplorerMedium
可见=真实
URL=”http://cfpsg1/plant/Reports/ScrapReport.aspx"
浏览网址
在IE.ReadyState ReadyState\u完成时执行此操作
多芬特
环
并尝试其他代码行以避免此问题也请参考


请根据您的请求添加ref Microsoft Internet控件和Microsoft HTML对象库

这是一个本地url,因为它不适合我加载。通过@QHarr使用snippet工具包含相关html将有所帮助。请参见编辑post@QHarr这不是我第一次遇到这个“文档”错误,但似乎是。文档是IE中的一个属性吗?所以我真的很困惑…谢谢你。我更改了那个部分,但它给出了与我描述的相同的错误。谢谢。这确实消除了错误,但在同一行出现了一个新的错误“automation error unspecified error”(自动错误未指定错误)
或者您可以参考答案中提供的链接,您可以理解为什么此错误触发此方法,但仍然是自动化错误未指定的错误。关闭所有其他IE实例并尝试执行代码,同时确保在项目中添加了引用。我已在引用中添加了internet控件。错误一直出现,直到刚才我把“Do while”改为“Do until”,没有错误。。。我理解readystate=4表示“结果正在返回”状态。我做的对吗?这个循环实际上意味着什么?