彰化一整天的論壇

 找回密碼
 立即註冊
查看: 19|回復: 0

IE DocumentComplete Event VBA

[複製鏈接]
發表於 2018-7-10 09:35:41 | 顯示全部樓層 |閱讀模式
IE DocumentComplete Event VBA
  1. Option Explicit

  2. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  3. Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  4. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  5. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  6. Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
  7. Public Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long
  8. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  9. Public Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
  10. Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
  11. Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  12. Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

  13. Public Const BM_CLICK = &HF5

  14. Public Sub IE_Automation(lst As Object)
  15.     Dim IE As SHDocVw.InternetExplorer
  16.     Dim sReportName As String
  17.     Dim bReady As Boolean
  18.     Dim i As Integer, j As Integer
  19.     Dim oCollection As Variant
  20.     Dim hwndIE As Long
  21.     Dim downloadcomplete As Boolean
  22.     Dim FileDownloadHandle As Long, OpenButtonHandle As Long, IePopupBarHandle As Long
  23.     Dim FileDownloadClassicPopup As Boolean

  24. lst.Clear

  25. lst.AddItem "Please wait......"
  26. lst.AddItem ""

  27.     Set IE = New InternetExplorer
  28.    
  29. With IE

  30.     '.Visible = True
  31.     .Navigate "www.example.com"

  32. While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 250

  33. lst.AddItem "Entering login details..."
  34.    
  35.     .Document.getelementbyID("UserName").Value = "UN"
  36.     .Document.getelementbyID("Password").Value = "PW"
  37.     .Document.getelementbyID("Submit").Click
  38.    
  39. While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 3500

  40.     lst.AddItem "LOGIN Successful..."
  41.     lst.AddItem "Setting report parameters..."

  42.     .Document.frames(0).Document.getelementbyID("Report").Value = "101"

  43. While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 250

  44.     .Document.frames(0).Document.getelementbyID("Report").fireevent "onchange"

  45. While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 2500

  46.     .Document.frames(0).Document.getelementbyID("fromDate").Value = Format(Now() - 1, "dd/mm/yyyy")
  47.     .Document.frames(0).Document.getelementbyID("toDate").Value = Format(Now() - 1, "dd/mm/yyyy")
  48.     sReportName = "JRE" & Format(Now(), "ddmmyyyyhhmmss")
  49.     .Document.frames(0).Document.getelementbyID("user_description").Value = sReportName
  50.     .Document.frames(0).Document.getelementbyID("sendreqbutton").Click

  51. While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 250

  52. lst.AddItem "Waiting for report to generate..."

  53. i = 1
  54.    
  55.     Do
  56.    
  57.         If .Document.frames(0).Document.getelementbyID("Desc" & i).Value = sReportName Then
  58.         
  59.             Set oCollection = .Document.frames(0).Document.getelementbyID("row" & i)
  60.             
  61.             If oCollection.Cells(5).innertext = "Completed" Then
  62.                
  63.                 .Document.frames(0).Document.getelementbyID("Desc" & i).Click
  64.                 bReady = True
  65.                 Exit Do
  66.             
  67.             Else
  68.                 lst.AddItem "Not done yet. Checking again..."
  69.                 .Document.frames(0).Document.parentwindow.execscript ("reloadPage()")
  70.                  While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 2500
  71.                 i = 0
  72.             
  73.             End If
  74.             
  75.         End If
  76.         
  77.         i = i + 1
  78.    
  79.     Loop Until bReady
  80.    
  81.     lst.AddItem "Report ready. Downloading..."
  82.    
  83.    Set oCollection = .Document.frames(0).Document.getelementsbyname("downloadtype")
  84.    
  85.     oCollection(0).Value = "2"
  86.    
  87.     Set oCollection = .Document.frames(0).Document.getelementsbyname("downloadbutton")
  88.    
  89.     oCollection(0).Click
  90.    
  91.     While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 2500

  92.     hwndIE = IE.hWnd

  93.     Do While downloadcomplete = False
  94.             
  95.             IePopupBarHandle = FindWindowEx(hwndIE, 0, "Frame Notification Bar", vbNullString)
  96.             IePopupBarHandle = FindWindowEx(IePopupBarHandle, 0, "DirectUIHWND", vbNullString)
  97.             
  98.             If IePopupBarHandle <> 0 And IE.ReadyState = 4 Then
  99.                 downloadcomplete = True
  100.             Else
  101.                 FileDownloadHandle = FindWindow("#32770", "File Download")
  102.                 If FileDownloadHandle <> 0 Then downloadcomplete = True: FileDownloadClassicPopup = True
  103.             End If
  104.             DoEvents
  105.             Sleep 250
  106.         Loop

  107.         Unload frmMain

  108.         If FileDownloadClassicPopup = True Then
  109.             
  110.             OpenButtonHandle = FindWindowEx(FileDownloadHandle, 0, "Button", "&Open")
  111.             SetForegroundWindow (OpenButtonHandle)
  112.             Sleep 1000
  113.             SendMessage OpenButtonHandle, BM_CLICK, 0, 0
  114.             DoEvents
  115.         Else
  116.             SetForegroundWindow (hwndIE)
  117.             Sleep 1000
  118.             SendKeys "%O", Wait:=True
  119.             DoEvents
  120.         End If

  121.     Set IE = Nothing

  122.     Application.StatusBar = False
  123.    
  124.     .Document.frames(0).Document.parentwindow.execscript ("setFooterCommand('signout'); closing=false;window.status='Logging Out - Please Wait.';top.actionFrame.location.href='/atom/logout.jsp?userid=UN';")
  125.    
  126.     While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Sleep 250: Wend: Sleep 2500
  127.    
  128.     .Quit
  129.    
  130.    
  131. End With

  132. End Sub


複製代碼
文章來源: https://www.experts-exchange.com ... lete-Event-VBA.html
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

 ㄚ母滴雞湯
 員林香純滴雞精

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)  |网站地图

GMT+8, 2018-7-21 00:19 , Processed in 0.048918 second(s), 10 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表