首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA:无法获取HTML id的内部文本

Excel VBA:无法获取HTML id的内部文本
EN

Stack Overflow用户
提问于 2017-08-19 06:44:13
回答 1查看 813关注 0票数 0

嗨:我正在使用excel从这个网页中获取值:https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html

如何获得excel VBA的代码以获得NIF、EJF、MOD和CEL的fiels?

我尝试使用getelementbyid("NIF")和"name“,但是没有结果。

谢谢!

Previos线程:Excel VBA: Get inner text of HTML table td

这是我使用的代码:

代码语言:javascript
复制
Sub AEAT()
  Dim IE As Object
  Application.ScreenUpdating = False
  Set IE = CreateObject("InternetExplorer.Application")

  IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"

  Application.Wait (Now + TimeValue("0:00:02"))


  IE.Document.getElementById("NIF").Value = Range("A1").Value
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("EJF").Value = "2016"
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("MOD").Value = "347"
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("CEL").Value = Range("a4").Value
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("env_button").Click

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-20 09:34:10

Okie,所以我在Windows中没有Excel,所以我使用VBScript测试了您的代码。所以解决办法应该有效。你有三个问题

  1. 导航不等待页面加载
  2. 您的站点安装一个ActiveX控件,需要首先加载该控件。
  3. 即使您修复了#1和#2,您也会得到一个“客户端断开连接”的异常,并且该对象将不再可用。

溶液

在IE设置中将站点添加到受信任的站点。

手动打开IE并安装ActiveX控件

代码语言:javascript
复制
Sub AEAT()
  Dim IE
  Set IE = CreateObject("InternetExplorer.Application")
  IE.visible = True
  hwnd = ie.hwnd
  IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"
  msgbox ("wait")

  Set oShell = CreateObject("Shell.Application")

  For Each Wnd In oShell.Windows
         If hwnd = Wnd.hwnd Then Set ie = Wnd
  Next 
  IE.Document.getElementById("NIF").Value = "123"
End Sub

Call AEAT

我已经将msgbox放到了插入“等待”的位置,但是您可以通过在excel中使用下面的命令来修复这个问题。

代码语言:javascript
复制
  Do
    DoEvents
  Loop Until ie.ReadyState = READYSTATE_COMPLETE

PS:参考以下网址

Internet Explorer VBA Automation Error: The object Invoked has disconnected from its clients

Error "The object invoked has disconnected from its clients" - automate IE 8 with python and win32com

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45768562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档