如何使用vba导航到已打开的IE窗口? [英] How to navigate to an already opened IE window using vba?
问题描述
因此,我一直在尝试使用VBA导航到已打开的IE窗口. 我找到了一篇帖子,解释了如何执行此操作.但是,我现在的主要问题是我对VBA不太熟悉,尤其是我的网站似乎没有文档标题.当我在调试器中检查该代码时,我在html代码中拥有的所有内容如下:
So I've been trying to navigate to an already opened IE window using VBA. I've found a post explaining what seems to be how to do this. However, my main problem now is that I'm not too familiar with VBA and my website in particular, doesn't seem to have a document title. All I have in the html code when i inspect it in the Debugger is the following:
<title></title>
我在这里错过了什么吗?还有什么其他方式可以更容易或更简单地访问网站?
Am I missing something here? Is there any other way to refer to the website that may be easier or simpler?
Sub demo()
Dim str_val As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "XYZ" & "*" Then
Set IE = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
MsgBox ("A matching webpage was found")
Set str_val = IE.document.getElementById("txtbox1")
str_val.Value = "demo text"
End If
End Sub
与已打开的特定IE窗口进行交互的VBA代码
VBA code to interact with specific IE window that is already open
推荐答案
这是我通常处理上述问题的方式,
This is the way I normally handle said problem,
Sub GetURLs()
' Uses MS HTML Controls
' and MS Internet Controls
Dim s As SHDocVw.InternetExplorer
Dim sw As SHDocVw.ShellWindows
Dim d As MSHTML.HTMLDocument
Set sw = New SHDocVw.ShellWindows
For Each s In sw
If TypeName(s) = "IWebBrowser2" And s.Name <> "Windows Explorer" Then
Debug.Print s.Name, s.LocationURL
Set d = s.Document
Debug.Print d.getElementsByTagName("TD")(0).innerhtml
End If
Next s
Set sw = Nothing
Set s = Nothing
End Sub
这篇关于如何使用vba导航到已打开的IE窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!