VBA:从现有打开的Internet Explorer选项卡中复制数据/文本



我正在尝试编写一个VBA代码,该代码允许我通过发送CTRL+a&CTRL+C使用SendKey方法,然后将其粘贴到Excel上的工作表中。我尝试过"从Web获取数据",但这不起作用,因为我需要输入用户名/密码,并且无法设置getElementbyId行。所以我找到了一些代码,可以扫描已经激活的Internet explorer会话的打开选项卡,但我想让VBA切换到匹配的选项卡,并从此网站复制文本。

Sub AAA()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next    ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_url Like "http://www.cnn.com" & "*" Then 'compare to find if the desired web page is already open
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
'Switch to the tab that matched and Copy the text from this website..
End If
End Sub

他是获得IE 句柄的一种方法

Option Explicit
Sub Sample()
Dim ShellApp As Object, ShellWindows As Object
Dim objIE As Object, objWin As Object
Dim ieCaption As String
Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()
'~~> Loop through the windows and check if it is IE
For Each objWin In ShellWindows
If (Not objWin Is Nothing) Then
ieCaption = objWin.Name
If ieCaption = "Internet Explorer" Then
Set objIE = objWin
Exit For
End If
End If
Next
Set ShellApp = Nothing
'~~> If IE is found then wirk with that object
If Not objIE Is Nothing Then
With objIE
Debug.Print .Document.Body.Innerhtml
End With
End If
End Sub

相关内容

  • 没有找到相关文章

最新更新