返回列表 上一主題 發帖

[發問] 代碼應該如何撰寫才能忽視 因查詢不到而產生的網頁訊息呢?

  1. Option Explicit

  2. #If Win64 = 1 Then
  3. Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal HWND As Long, ByVal wCmd As Long) As Long
  4. Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
  5. #Else
  6. Private Declare Function GetWindow Lib "user32" (ByVal HWND As Long, ByVal wCmd As Long) As Long
  7. Private Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
  8. #End If

  9. Private Const GW_HWNDNEXT = 2
  10. Private Const GW_CHILD = 5

  11. Sub ex()
  12.     Dim lHwndChild&
  13.    
  14.     With CreateObject("InternetExplorer.Application")
  15.         .Visible = True
  16.         .Navigate "https://dl.dropboxusercontent.com/u/40393708/test1.htm"
  17.         Do While .Busy Or .ReadyState <> 4
  18.             DoEvents
  19.             If 3 = .ReadyState Then
  20.                 lHwndChild = GetWindow(.HWND, GW_CHILD)
  21.                 Do While lHwndChild
  22.                     SetForegroundWindow .HWND 'HWND is the HWND of the myIEWindow
  23.                     Application.SendKeys "~", True
  24.                     lHwndChild = GetWindow(lHwndChild, GW_HWNDNEXT)
  25.                 Loop
  26.             End If
  27.         Loop
  28.     End With
  29.    
  30. End Sub
複製代碼
回復 8# joey0415

TOP

回復 10# joey0415
大概就是確認IE是否有任何子視窗顯示(ex:訊息視窗),有就將IE切換成最上層顯示後再送出ENTER鍵將訊息視窗關閉。
設定值請參考下列網址:
https://msdn.microsoft.com/zh-tw/ms633515
另外我寫的過於簡化,設想如果發生子視窗無法用ENTER鍵關閉的情況,你最好加上錯誤處理機制(ex:送出ENTER鍵幾次無效後就不再執行改顯示錯誤訊息),不然有可能會進入死迴圈...

TOP

回復 12# joey0415
我用你上面貼的程式碼及GBKEE版主教的方法都沒有出現問題(用 Win7 + IE11 + Office2013),所以我也不曉得出了什麼問題...
這問題如果短時間內無法解決你又急需這些資料,下列網址可以抓到最新一期的資料(csv檔):
http://data.gov.tw/node/11452

TOP

回復 14# joey0415
另一種方式,試試看...
  1. Option Explicit

  2. #If Win64 Then
  3. Private Declare PtrSafe Function GetWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As LongPtr
  4. Private Declare PtrSafe Function SetForegroundWindow Lib "USER32" (ByVal hWnd As Long) As Long
  5. Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
  6. Private Declare PtrSafe Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
  7. Private Declare PtrSafe Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
  8. #Else
  9. Private Declare Function GetWindow Lib "user32" (ByVal HWND As Long, ByVal wCmd As Long) As Long
  10. Private Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
  11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  12. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  13. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  14. #End If

  15. Private Const WM_CLOSE = &H10
  16. Private Const GW_HWNDNEXT = 2
  17. Private Const GW_CHILD = 5
  18. Private Const BM_CLICK = &HF5

  19. Sub 點擊()
  20.     Cells.Clear
  21.     Dim hWndChild, hWndPopup, x, Retries As Integer: Retries = 10
  22.    
  23.     With CreateObject("InternetExplorer.Application")
  24.         .Visible = True
  25.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  26.         
  27.         Do While .Busy Or .ReadyState <> 4  '等候網頁下載完畢
  28.             DoEvents
  29.         Loop
  30.         Set x = .document.all.tags("option") '查下拉選單
  31.         x(7).Selected = True '選取該子項目
  32.         
  33.         .document.getElementsByName("StockNo")(0).Value = "6456"
  34.         .document.getElementsByName("sub")(0).Click
  35.         
  36.         Do While (.Busy Or .ReadyState <> 4) And 0 < Retries
  37.         
  38.             hWndChild = GetWindow(.hWnd, GW_CHILD)
  39.         
  40.             If 0 <> hWndChild Then
  41.                 Do While hWndChild
  42.                     hWndPopup = FindWindow("#32770", "網頁訊息") '#32770 The class for a dialog box
  43.                     If 0 <> hWndPopup Then
  44.                         SendMessage FindWindowEx(hWndPopup, 0, "Button", "確定"), BM_CLICK, 0, 0
  45.                     Else
  46.                         Exit Do
  47.                     End If
  48.                     'SetForegroundWindow .hWnd 'HWND is the HWND of the myIEWindow
  49.                     'Application.SendKeys "~", True
  50.                     hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
  51.                 Loop
  52.             
  53.             End If
  54.             Application.Wait Now + #12:00:01 AM#
  55.             DoEvents
  56.             Retries = Retries - 1
  57.         Loop
  58.         If 0 = Retries Then
  59.             AppActivate Application.Caption
  60.             MsgBox "無法關閉訊息視窗!", vbCritical
  61.             Exit Sub
  62.         End If
  63.         '-------------------以下為失焦處,無法點擊
  64.         Stop
  65.         .document.getElementsByName("StockNo")(0).Value = "1101"
  66.         .document.getElementsByName("sub")(0).Click
  67.             
  68.         Do While .Busy Or .ReadyState <> 4  '等候網頁下載完畢
  69.            DoEvents
  70.         Loop
  71.         
  72.         Stop
  73.         .Quit
  74.     End With

  75. End Sub
複製代碼

TOP

        靜思自在 : 我們要做好社會的環保,也要做好內心的環保。
返回列表 上一主題