【问题标题】:How to access the Web using VBA? Please check my code如何使用 VBA 访问 Web?请检查我的代码
【发布时间】:2020-12-28 02:29:36
【问题描述】:

为了改进重复性工作,我尝试使用VBA访问公司正在使用的网站。

所以,我使用 VBA 编写代码。我检查它可以访问正常的网站,如谷歌,youtube...... 但是,我不知道为什么它无法访问公司网站。

VBA 停止了这一行 Set HTMLDoc = IE_ctrl.document

先谢谢你了。

我在 Normal 和 company 站点之间检查了一个不同的东西(VBA 本地值、类型)。 请查看以下 2 张图片。

Sub a()

Dim IE_ctrl As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim input_Data As IHTMLElement
Dim URL As String
URL = "https://www.google.com"

Set IE_ctrl = New InternetExplorer
IE_ctrl.Silent = True
IE_ctrl.Visible = True
IE_ctrl.navigate URL

Wait_Browser IE_ctrl

Set HTMLDoc = IE_ctrl.document

Wait_Browser IE_ctrl

Set input_Data = HTMLDoc.getElementsByClassName("text").Item
input_Data.Click

End Sub
Sub Wait_Browser(Browser As InternetExplorer, Optional t As Integer = 1)
 
While Browser.Busy
DoEvents
Wend

Application.Wait DateAdd("s", t, Now)
 
End Sub
  1. 正常站点(运行良好。) enter image description here
  2. 公司网站(操作错误。) enter image description here

【问题讨论】:

  • google 的 IE_control 值是 InternetExplorer/WebBrowser2,但对于您的站点,它只是 InternetExplorer。我不知道这到底是什么意思。第一个想法是,当你完成它们时,确保你 Set 对象到 Nothing。当他们为下一个网站重置时,也许这会有所帮助。还要确保 URL 正确。

标签: html excel vba


【解决方案1】:

你可以试试下面的代码。请阅读 cmets。我不能再说了,因为我不知道页面或者页面的html。

Sub a()

'Use late binding for what you need
Dim ie As Object
Dim nodeInputData As Object
Dim url As String

  url = "https://www.google.com"
  
  'Use the windows GUID to initialize the Internet Explorer, if you
  'want to get access to a company page. This helps if there are
  'security rules you can't access over other ways of initializing IE
  'This don't work in most cases for pages in the "real" web
  'Read here for more infos:
  'https://blogs.msdn.microsoft.com/ieinternals/2011/08/03/default-integrity-level-and-automation/
  Set ie = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
  ie.Visible = True
  ie.navigate url
  'Waiting for the document to load
  Do Until ie.readyState = 4: DoEvents: Loop
  'If necessary, if there is dynamic content that must be loaded,
  'after the ie reports, loading was ready
  '(The last three values are: hours, minutes, seconds)
  Application.Wait (Now + TimeSerial(0, 0, 1))
  
  'I don't know your html. If you only want to click a button,
  'you don't need a varable
  'ie.document.getElementsByClassName("text")(0).Click
  'will do the same like
  Set nodeInputData = ie.document.getElementsByClassName("text")(0)
  nodeInputData.Click
  
  'A short explanation of getElementsByClassName() and getElementsByTagName():
  'Both methods create a node collection of all html elements that was found
  'by the creteria in the brackets. This is because there can be any number of
  'html elements with specified class names or tag names. If, for example,
  '3 html elements with the class name "Text" were found, a node collection
  'with three elements is created by getElementsByClassName("Text").
  'These have the indices 0 to 2, as in an array. The individual elements are
  'also addressed via these indices. They are indicated in round brackets.
End Sub

【讨论】:

  • 感谢您的回复。但我试过了,有一些错误。 宏无法检测到此评论。
猜你喜欢
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
  • 2022-12-07
  • 1970-01-01
相关资源
最近更新 更多