Sub example_01()
Dim objIE As Object, txt$
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate ("https://mail.ru/?from=logout&ref=main")
Do While objIE.Busy
Application.Wait DateAdd("s", 4, Now)
Loop
objIE.Visible = True
With objIE.document
.getElementsByName("Login").Item(0).Value = "АдресЯщика"
.getElementsByName("Password").Item(0).Value = "Пароль"
.getElementByID("mailbox__auth__button").Click
End With
Do While objIE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
MsgBox "Зашли в почту", 64
' вдруг захотелось считать всю страницу в текстовую переменную
txt = objIE.document.body.innerHTML
MsgBox "Длина строки " & Len(txt)
Call GetStr(txt)
End Sub
Sub GetStr(s As String) 'получаем строку
Dim sp, x(), i&
sp = Split(s, vbLf)
ReDim x(0 To UBound(sp), 0 To 0)
For i = 0 To UBound(sp)
'обрезаем слишком длинные строки до, например, 120 символов
x(i, 0) = Left(sp(i), 120)
Next i
Sheets("Sheet1").Range("A1").Resize(UBound(sp) + 1).Value = x()
End Sub
Sub example_02() 'если IE уже открыт на нужной странице
Dim j&, i&, URL As String, IE As Object
'URL = "https://mail.ru/?from=logout&ref=main"
URL = "https://mail.ru/?from=logout"
Set IE = Get_IE_Window3(URL)
If IE Is Nothing Then MsgBox "IE не соответствует адресу " & URL, 64: Exit Sub
With IE.document
.getElementsByName("Login").Item(0).Value = "АдресЯщика"
.getElementsByName("Password").Item(0).Value = "Пароль"
.getElementByID("mailbox__auth__button").Click
End With
Set IE = Nothing
End Sub
Private Function Get_IE_Window3(sURL) As Object
'Look for an IE browser window or tab already open at the specified URL and, if found, return that browser
'as an InternetExplorer object. Otherwise return Nothing
Dim Shell As Object, IE As Object, i As Variant 'Variant!!!!
Set Shell = CreateObject("Shell.Application")
i = 0
Set Get_IE_Window3 = Nothing
Do While i < Shell.Windows.Count And Get_IE_Window3 Is Nothing
Set IE = Shell.Windows.Item(i)
If Not IE Is Nothing Then
'If InStr(IE.FullName, "IEXPLORE.EXE") Then
If TypeName(IE.document) = "HTMLDocument" Then
If InStr(IE.LocationURL, sURL) > 0 Then Set Get_IE_Window3 = IE
End If
End If
i = i + 1
Loop
Set Shell = Nothing
End Function
'или по известному адресу просто читаем текст страницы
Sub example_03()
Dim sRf$, txt$
sRf = "https://mail.ru/?from=logout&ref=main"
txt = GetPageSource(sRf)
Call GetStr(txt)
End Sub
Function GetPageSource(ByVal URL As String) As Variant 'from Leith Ross
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, True
.Send
While .readyState <> 4: DoEvents: Wend
' если соединение не произошло
If .statusText <> "OK" Then
' сообщение об ошибке
GetPageSource = "ERROR " & .Status & " - " & .statusText
Exit Function
End If
GetPageSource = .responseText
End With
End Function
|